shuizhuyu asked for a global shortcut to switch accounts. v0.1.2143's Quick panel shortcut opens the tray panel, but the panel had no account switch: switchable() returned null there, because the panel reads no provider list (a fresh /api/providers reads every sign-in, seconds after a start, #1519). Now a Claude Code or Codex not connected has an "account" line in its opened panel row (switchEntry, the window's Switch accounts only menu). The line is drawn from the list kept from the last read (GET /api/providers?last=1, no keychain read; panelAccounts in app.js), read fresh only when nothing is kept. Opening the menu does one fresh read and opens on what it says. Manage accounts… and a lapsed pick open the window's Providers (window/main?view=providers). The provider and login actions now keep their answer as the last list (keepProviders), so a switch made in the window or the panel is in the next kept read. app.js stays under its budget with no raise: my own comments and two older ones (the usage accounts note, the sidebar note) were cut down. Run: - GUI, Windows box (xiaomi-gpu, win-run), Chromium + WebKit: 31 files (panel-*, panel-switch-accounts, agent-switch-accounts, switch-only, claude-switch-only, keep-login, agent-failover, agent-row-disclose, getting-started, click-scroll, i18n-keys, gui-zh-tw, gui-ja, gui-de): 503 of 503 pass. Not the whole GUI suite. - mote: no browser could launch there (pboard down, #4125; every WebKit and later Chromium launch timed out at 180s), so the GUI runs there were cut short. agent-panel-name-width, red there in both engines, is red on origin/main too (#4304, #4203), and this change doesn't draw the closed rows it measures. - mote: go vet ./internal/gui; go test -run 'TestKeptProvidersFollowActions| TestLastKnownProviders|Budget|TestProvider' ./internal/gui ok (includes TestCoreScriptBudget). gofmt -l clean. GOOS=windows go vet ./... ok. GOOS=linux go vet ./... fails only in wails/v3 pkg/application (cgo off when cross-compiling), the same on origin/main32f5c3369(#4406). At the pushed head (rebased on origin/main32f5c3369, whose switchEntry now leaves setup-token logins out of the menu; kept): the Go run above again ok, TestCoreScriptBudget at the new 1,536,586 included, app.js 1,314,856 of 1,315,316; on the Windows box, panel-switch-accounts, agent-switch-accounts, switch-only, claude-switch-only, keep-login, agent-failover, i18n-keys, panel-fold and panel-effort: 178 of 178. Broken by hand: - panel-switch-accounts on origin/main's assets: 12 of 12 red in Chromium (no account line). - Panel always reading fresh (laziness removed): 11 of 12 red. - keepProviders removed from the provider action: TestKeptProvidersFollowActions red. Not tried: real Claude Code or Codex accounts; the real Wails panel opened by the shortcut; the login/{action} keep has no test of its own.
146 lines
4.3 KiB
Go
146 lines
4.3 KiB
Go
package gui
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http/httptest"
|
|
"os"
|
|
"runtime"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
func forgetLastProviders() {
|
|
lastProviders.Lock()
|
|
lastProviders.kept, lastProviders.read = nil, false
|
|
lastProviders.Unlock()
|
|
}
|
|
|
|
// #1519 (fatkun): the Providers page draws the list kept from the last
|
|
// read at once (?last=1), this run's or the run before's, and the fresh
|
|
// one after; none kept is null. What can hold a secret (a header's value,
|
|
// a proxy's password, the raw key) never reaches the file.
|
|
func TestLastKnownProviders(t *testing.T) {
|
|
sandboxHome(t)
|
|
forgetLastProviders()
|
|
t.Cleanup(forgetLastProviders)
|
|
if _, err := provider.Add(provider.Provider{ID: "own", Name: "Own", Chat: "http://127.0.0.1:1/v1", Key: "sk-raw-key-1519",
|
|
Models: []string{"m1"}, Headers: map[string]string{"X-Token": "hdr-secret-1519"}, Proxy: "http://u:proxy-secret-1519@127.0.0.1:2"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
srv := Handler(nil, nil)
|
|
get := func(path string) string {
|
|
t.Helper()
|
|
rec := httptest.NewRecorder()
|
|
srv.ServeHTTP(rec, httptest.NewRequest("GET", path, nil))
|
|
if rec.Code != 200 {
|
|
t.Fatalf("%s: %d %s", path, rec.Code, rec.Body)
|
|
}
|
|
return strings.TrimSpace(rec.Body.String())
|
|
}
|
|
type list struct {
|
|
Last bool `json:"last"`
|
|
Providers []struct {
|
|
ID string `json:"id"`
|
|
} `json:"providers"`
|
|
}
|
|
has := func(l list, id string) bool {
|
|
for _, p := range l.Providers {
|
|
if p.ID == id {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
if b := get("/api/providers?last=1"); b != "null" {
|
|
t.Fatalf("nothing kept yet: want null, got %.200s", b)
|
|
}
|
|
var fresh list
|
|
json.Unmarshal([]byte(get("/api/providers")), &fresh)
|
|
if fresh.Last || !has(fresh, "own") {
|
|
t.Fatalf("fresh list: %+v", fresh)
|
|
}
|
|
file, err := os.ReadFile(lastProvidersPath())
|
|
if err != nil {
|
|
t.Fatalf("the read was not kept: %v", err)
|
|
}
|
|
for _, s := range []string{"sk-raw-key-1519", "hdr-secret-1519", "proxy-secret-1519"} {
|
|
if strings.Contains(string(file), s) {
|
|
t.Errorf("the kept list holds %s", s)
|
|
}
|
|
}
|
|
if fi, err := os.Stat(lastProvidersPath()); err == nil && runtime.GOOS != "windows" && fi.Mode().Perm()&0o077 != 0 {
|
|
t.Errorf("the kept list is readable by others: %v", fi.Mode())
|
|
}
|
|
|
|
// magpie starts again: the list kept is served, marked as such
|
|
forgetLastProviders()
|
|
var last list
|
|
if err := json.Unmarshal([]byte(get("/api/providers?last=1")), &last); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !last.Last || !has(last, "own") {
|
|
t.Fatalf("kept list after a start: %+v", last)
|
|
}
|
|
|
|
// kept by another version: not drawn
|
|
b, _ := os.ReadFile(lastProvidersPath())
|
|
os.WriteFile(lastProvidersPath(), []byte(strings.Replace(string(b), `"version":"`, `"version":"other-`, 1)), 0o600)
|
|
forgetLastProviders()
|
|
if b := get("/api/providers?last=1"); b != "null" {
|
|
t.Fatalf("another version's list served: %.200s", b)
|
|
}
|
|
}
|
|
|
|
// The tray panel's account switch (shuizhuyu on X) draws the kept list,
|
|
// so a change made through a provider or login action is kept at once,
|
|
// not only on the next GET /api/providers.
|
|
func TestKeptProvidersFollowActions(t *testing.T) {
|
|
sandboxHome(t)
|
|
forgetLastProviders()
|
|
t.Cleanup(forgetLastProviders)
|
|
if _, err := provider.Add(provider.Provider{ID: "own", Name: "Own", Chat: "http://127.0.0.1:1/v1", Key: "sk-own", Models: []string{"m1"}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
srv := Handler(nil, nil)
|
|
do := func(method, path, body string) string {
|
|
t.Helper()
|
|
rec := httptest.NewRecorder()
|
|
srv.ServeHTTP(rec, httptest.NewRequest(method, path, strings.NewReader(body)))
|
|
if rec.Code != 200 {
|
|
t.Fatalf("%s %s: %d %s", method, path, rec.Code, rec.Body)
|
|
}
|
|
return rec.Body.String()
|
|
}
|
|
off := func() bool {
|
|
t.Helper()
|
|
var l struct {
|
|
Last bool `json:"last"`
|
|
Providers []struct {
|
|
ID string `json:"id"`
|
|
Off bool `json:"off"`
|
|
} `json:"providers"`
|
|
}
|
|
if err := json.Unmarshal([]byte(do("GET", "/api/providers?last=1", "")), &l); err != nil || !l.Last {
|
|
t.Fatalf("kept list: %+v %v", l, err)
|
|
}
|
|
for _, p := range l.Providers {
|
|
if p.ID == "own" {
|
|
return p.Off
|
|
}
|
|
}
|
|
t.Fatal("own is not in the kept list")
|
|
return false
|
|
}
|
|
do("GET", "/api/providers", "")
|
|
if off() {
|
|
t.Fatal("own kept as off before it was switched off")
|
|
}
|
|
do("POST", "/api/provider/off", `{"id":"own"}`)
|
|
if !off() {
|
|
t.Error("the kept list still has own on after it was switched off")
|
|
}
|
|
}
|