jtbbtnt54249, on LolingVerse's agy thread: "很多都这样,退出有时候又要重新开启".
Several agents' switches were off after quitting magpie and had to be
turned on again by hand.
Looked for a shared path that flips agents off. The row's switch is
Wired() alone, and nothing automatic disconnects an agent except a
removed plugin, the gateway pause (which reconnects on resume) and
Put agents back when quitting (which reconnects at start). The quit
question's Disconnect and quit, though, ran each row's Disconnect,
which leaves the disconnectedKey mark: every agent it listed stayed off
at every start after, and RewireOnStart skips a marked agent. Users
choose it so their agents work while magpie is closed, not to leave
magpie for good.
Now Disconnect and quit posts quit/disconnect (quitDisconnect), which
runs agent.RestoreOnQuit on the agents the question lists: their own
files back, no mark, recorded in quit.restored, so the next start's
RewireOnStart connects them again, the setting on or off. This covers
every agent the question lists: every detected agent wired to magpie,
built-in, plugin and WSL ones alike. A failure answers an error naming
the agent and magpie stays open. Its tooltip says magpie connects them
again when it opens (en, zh, zh-TW, ja, de).
Run at this tree (parent 99a54fa87, origin/main just fetched):
- TestQuitDisconnectComesBackOnStart red on the old api.go and
restoreonquit.go (404 on POST /api/quit/disconnect), green after.
- mote: go test -count=1 ./internal/gui ok (145s).
- mica: node --test quit-ask, quit-ask-setting, restore-on-quit,
i18n-keys, i18n-en-no-cjk: 178 of 178 pass, chromium and webkit,
every language.
- gofmt -l clean; go vet ./internal/gui on darwin and GOOS=windows ok.
Linux vet of internal/gui needs GTK/WebKitGTK headers, which neither
mote nor magpie-desktop has; not run.
Not tried: a real quit of the app with real agents; the agent-level
RestoreOnQuit then RewireOnStart path is covered by
TestRestoreOnQuitAndRewireOnStart. Agents disconnected this way by an
older magpie keep their mark and stay off until switched on once.
269 lines
9.6 KiB
Go
269 lines
9.6 KiB
Go
package gui
|
||
|
||
import (
|
||
"encoding/json"
|
||
"errors"
|
||
"net/http/httptest"
|
||
"strings"
|
||
"testing"
|
||
"time"
|
||
|
||
"github.com/yetone/magpie/internal/agent"
|
||
"github.com/yetone/magpie/internal/settings"
|
||
)
|
||
|
||
// A Quit the user chose while agents go through magpie asks first
|
||
// (Aisiverto on X: Codex's 502s after magpie was quit, with nothing to
|
||
// say why). The window's Quit reaches AskQuit; the question's own Quit
|
||
// (now=1) quits; a browser's page (magpie web) has no window to ask in
|
||
// and quits.
|
||
func TestWindowQuitAsksFirst(t *testing.T) {
|
||
quits := 0
|
||
asked := make(chan struct{}, 4)
|
||
h := webPageHandler(t, askingHost{webHost{quit: func() { quits++ }}, asked}, nil)
|
||
post := func(url string) {
|
||
t.Helper()
|
||
r := httptest.NewRecorder()
|
||
h.ServeHTTP(r, httptest.NewRequest("POST", url, nil))
|
||
if r.Code != 204 {
|
||
t.Fatalf("%s: %d %s", url, r.Code, r.Body)
|
||
}
|
||
}
|
||
|
||
post("/api/window/quit")
|
||
select {
|
||
case <-asked:
|
||
case <-time.After(5 * time.Second):
|
||
t.Fatal("the window's Quit didn't ask (AskQuit never ran)")
|
||
}
|
||
if quits != 0 {
|
||
t.Fatalf("the window's Quit quit %d times without asking", quits)
|
||
}
|
||
|
||
post("/api/window/quit?now=1")
|
||
if quits != 1 {
|
||
t.Fatalf("the question's Quit (now=1) quit %d times, want 1", quits)
|
||
}
|
||
select {
|
||
case <-asked:
|
||
t.Fatal("the question's own Quit asked again")
|
||
default:
|
||
}
|
||
|
||
web := webPageHandler(t, webHost{quit: func() { quits++ }}, nil)
|
||
r := httptest.NewRecorder()
|
||
web.ServeHTTP(r, httptest.NewRequest("POST", "/api/window/quit", nil))
|
||
if quits != 2 {
|
||
t.Fatalf("a browser's Quit quit %d times, want at once", quits-1)
|
||
}
|
||
}
|
||
|
||
// askingHost is a host that asks, as the app's does.
|
||
type askingHost struct {
|
||
webHost
|
||
asked chan struct{}
|
||
}
|
||
|
||
func (h askingHost) AskQuit() { h.asked <- struct{}{} }
|
||
|
||
// The question names the agents magpie is in, and only those.
|
||
func TestQuitListsWiredAgents(t *testing.T) {
|
||
yes := func() bool { return true }
|
||
got := wiredAgents([]*agent.Agent{
|
||
{ID: "codex", Name: "Codex", Icon: "openai", Joined: yes},
|
||
{ID: "claude", Name: "Claude Code", Icon: "claude"},
|
||
{ID: "hermes", Name: "Hermes", Icon: "hermes", Beside: yes},
|
||
})
|
||
want := []quitAgentJSON{{ID: "codex", Name: "Codex", Icon: "openai"}, {ID: "hermes", Name: "Hermes", Icon: "hermes"}}
|
||
if len(got) != len(want) || got[0] != want[0] || got[1] != want[1] {
|
||
t.Fatalf("wired agents = %+v, want %+v", got, want)
|
||
}
|
||
// none is an empty list, which the page reads as "quit at once"
|
||
if b, _ := json.Marshal(wiredAgents(nil)); string(b) != "[]" {
|
||
t.Fatalf("no agents marshal as %s, want []", b)
|
||
}
|
||
}
|
||
|
||
// GET /api/quit is what the question reads.
|
||
func TestQuitAPIListsAgents(t *testing.T) {
|
||
old := quitAgents
|
||
t.Cleanup(func() { quitAgents = old })
|
||
quitAgents = func() []quitAgentJSON { return []quitAgentJSON{{ID: "codex", Name: "Codex", Icon: "openai"}} }
|
||
r := httptest.NewRecorder()
|
||
webPageHandler(t, webHost{}, nil).ServeHTTP(r, httptest.NewRequest("GET", "/api/quit", nil))
|
||
var got struct{ Agents []quitAgentJSON }
|
||
if err := json.Unmarshal(r.Body.Bytes(), &got); err != nil || len(got.Agents) != 1 || got.Agents[0].ID != "codex" {
|
||
t.Fatalf("GET /api/quit = %d %s (%v)", r.Code, r.Body, err)
|
||
}
|
||
}
|
||
|
||
// With no gateway served here (another magpie serves it, and goes on
|
||
// serving the agents) a Quit cuts nobody off.
|
||
func TestQuitAgentsNoneWithoutGateway(t *testing.T) {
|
||
if served.Load() != nil {
|
||
t.Skip("a gateway is served in this test binary")
|
||
}
|
||
if got := quitAgents(); len(got) != 0 {
|
||
t.Fatalf("with no gateway served here, a Quit cuts off %+v", got)
|
||
}
|
||
}
|
||
|
||
// With agents connected the first Quit asks; a second within a minute of
|
||
// the question quits (the user was told, and it is the way out should the
|
||
// window not show the question); later, it asks again.
|
||
func TestQuitNow(t *testing.T) {
|
||
now := time.Date(2026, 11, 1, 12, 0, 0, 0, time.UTC)
|
||
asked := now.Add(-10 * time.Second).UnixNano()
|
||
for _, c := range []struct {
|
||
name string
|
||
asked int64
|
||
wired int
|
||
want bool
|
||
}{
|
||
{"nothing connected", 0, 0, true},
|
||
{"nothing connected, asked before", asked, 0, true},
|
||
{"connected, never asked", 0, 2, false},
|
||
{"connected, asked 10s ago", asked, 1, true},
|
||
{"connected, asked 2m ago", now.Add(-2 * time.Minute).UnixNano(), 1, false},
|
||
} {
|
||
if got := quitNow(c.asked, now, c.wired); got != c.want {
|
||
t.Errorf("%s: quitNow = %v, want %v", c.name, got, c.want)
|
||
}
|
||
}
|
||
}
|
||
|
||
// "Don't ask again" (zhujiu on Discord): the question's Quit sent with
|
||
// noAsk=1 saves settings.QuitNoAsk and quits; from then on a Quit with
|
||
// agents connected quits at once, until Settings › General › Ask before
|
||
// quitting turns the question back on.
|
||
func TestQuitDontAskAgain(t *testing.T) {
|
||
sandboxHome(t)
|
||
old := quitAgents
|
||
t.Cleanup(func() { quitAgents = old })
|
||
quitAgents = func() []quitAgentJSON { return []quitAgentJSON{{ID: "codex", Name: "Codex", Icon: "openai"}} }
|
||
now := time.Now()
|
||
if quitAtOnce(0, now) {
|
||
t.Fatal("with Codex connected, the first Quit quit without asking")
|
||
}
|
||
|
||
quits := 0
|
||
h := webPageHandler(t, askingHost{webHost{quit: func() { quits++ }}, make(chan struct{}, 1)}, nil)
|
||
r := httptest.NewRecorder()
|
||
h.ServeHTTP(r, httptest.NewRequest("POST", "/api/window/quit?now=1&noAsk=1", nil))
|
||
if r.Code != 204 || quits != 1 {
|
||
t.Fatalf("the question's Quit with Don't ask again: %d %s, %d quits", r.Code, r.Body, quits)
|
||
}
|
||
if !settings.Load().QuitNoAsk {
|
||
t.Fatal("Don't ask again wasn't saved")
|
||
}
|
||
if !quitAtOnce(0, now) {
|
||
t.Fatal("after Don't ask again, a Quit with Codex connected still asks")
|
||
}
|
||
|
||
if err := settings.SetQuitNoAsk(false); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
if quitAtOnce(0, now) {
|
||
t.Fatal("with Ask before quitting on again, a Quit didn't ask")
|
||
}
|
||
// a plain Quit from the question saves nothing
|
||
h.ServeHTTP(httptest.NewRecorder(), httptest.NewRequest("POST", "/api/window/quit?now=1", nil))
|
||
if settings.Load().QuitNoAsk {
|
||
t.Fatal("the question's Quit without Don't ask again saved it")
|
||
}
|
||
}
|
||
|
||
// tudjam17795 on X: with Settings › General › Put agents back when
|
||
// quitting on, the app's quit restores the connected agents and its next
|
||
// start connects them again; off (the default), a quit leaves them as
|
||
// they are. With it on a Quit needs no question, and a gateway served by
|
||
// another magpie leaves its agents alone.
|
||
func TestRestoreAgentsOnQuit(t *testing.T) {
|
||
sandboxHome(t)
|
||
old := [...]any{servedHere, quitDetect, quitRestore, startAgents, startRewire}
|
||
t.Cleanup(func() {
|
||
servedHere, quitDetect, quitRestore = old[0].(func() bool), old[1].(func() []*agent.Agent), old[2].(func([]*agent.Agent) ([]string, error))
|
||
startAgents, startRewire = old[3].(func() []*agent.Agent), old[4].(func([]*agent.Agent) ([]string, error))
|
||
})
|
||
restored, rewired := 0, 0
|
||
servedHere = func() bool { return true }
|
||
quitDetect = func() []*agent.Agent { return nil }
|
||
quitRestore = func([]*agent.Agent) ([]string, error) { restored++; return nil, nil }
|
||
startAgents = func() []*agent.Agent { return nil }
|
||
startRewire = func([]*agent.Agent) ([]string, error) { rewired++; return nil, nil }
|
||
oldAgents := quitAgents
|
||
t.Cleanup(func() { quitAgents = oldAgents })
|
||
quitAgents = func() []quitAgentJSON { return []quitAgentJSON{{ID: "codex"}} }
|
||
if quitAtOnce(0, time.Now()) {
|
||
t.Fatal("off by default, a Quit with Codex connected quit without asking")
|
||
}
|
||
|
||
restoreAgentsOnQuit()
|
||
if restored != 0 {
|
||
t.Fatal("off by default, a quit put the agents back")
|
||
}
|
||
s := settings.Load()
|
||
s.RestoreOnQuit = true
|
||
if err := settings.Save(s); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
servedHere = func() bool { return false }
|
||
restoreAgentsOnQuit()
|
||
if restored != 0 {
|
||
t.Fatal("another magpie serves the gateway, yet the quit put its agents back")
|
||
}
|
||
servedHere = func() bool { return true }
|
||
restoreAgentsOnQuit()
|
||
if restored != 1 {
|
||
t.Fatalf("with the setting on, restored %d times", restored)
|
||
}
|
||
// nothing to ask: agents are put back
|
||
if !quitAtOnce(0, time.Now()) {
|
||
t.Fatal("with the setting on a Quit asks")
|
||
}
|
||
rewireAgentsOnStart()
|
||
if rewired != 1 {
|
||
t.Fatalf("a start rewired %d times", rewired)
|
||
}
|
||
}
|
||
|
||
// jtbbtnt54249 on X: after Disconnect and quit, every agent's switch was
|
||
// off when magpie opened again. The question's Disconnect and quit now
|
||
// puts the agents back as Put agents back when quitting does, setting on
|
||
// or off (the next start connects them again: agent.RewireOnStart), not
|
||
// with each row's Disconnect, whose mark kept them off for good. One that
|
||
// can't be put back answers an error and nothing quits.
|
||
func TestQuitDisconnectComesBackOnStart(t *testing.T) {
|
||
sandboxHome(t)
|
||
old := [...]any{quitDetect, quitRestore}
|
||
t.Cleanup(func() {
|
||
quitDetect, quitRestore = old[0].(func() []*agent.Agent), old[1].(func([]*agent.Agent) ([]string, error))
|
||
})
|
||
detected := []*agent.Agent{{ID: "codex", Name: "Codex"}, {ID: "claude", Name: "Claude Code"}}
|
||
quitDetect = func() []*agent.Agent { return detected }
|
||
var got [][]*agent.Agent
|
||
var fails error
|
||
quitRestore = func(as []*agent.Agent) ([]string, error) { got = append(got, as); return []string{"codex"}, fails }
|
||
quits := 0
|
||
h := webPageHandler(t, askingHost{webHost{quit: func() { quits++ }}, make(chan struct{}, 1)}, nil)
|
||
|
||
r := httptest.NewRecorder()
|
||
h.ServeHTTP(r, httptest.NewRequest("POST", "/api/quit/disconnect", nil))
|
||
if r.Code != 200 || len(got) != 1 || len(got[0]) != 2 {
|
||
t.Fatalf("Disconnect and quit: %d %s, put back %d times", r.Code, r.Body, len(got))
|
||
}
|
||
if settings.Load().RestoreOnQuit {
|
||
t.Fatal("it turned Put agents back when quitting on")
|
||
}
|
||
if quits != 0 {
|
||
t.Fatal("it quit by itself: the page quits after it")
|
||
}
|
||
|
||
fails = errors.New("Claude Code: config is locked")
|
||
r = httptest.NewRecorder()
|
||
h.ServeHTTP(r, httptest.NewRequest("POST", "/api/quit/disconnect", nil))
|
||
if r.Code < 400 || !strings.Contains(r.Body.String(), "Claude Code: config is locked") {
|
||
t.Fatalf("a failed put back: %d %s", r.Code, r.Body)
|
||
}
|
||
}
|