Files
magpie/internal/gui/quitask_test.go
T
yetone 8826a15ea4 gui: a Quit while agents go through magpie asks first, naming them (Aisiverto on X)
Aisiverto on X (Windows, v0.1.2519) wired Codex to magpie and quit magpie.
Codex then retried "unexpected status 502 Bad Gateway ... url:
http://127.0.0.1:3425/backend-api/codex/responses" five times and gave up,
and it took a long search to find magpie's openai_base_url in config.toml.

A Quit the user chooses (the tray's Quit, the panel's and window's Quit,
Command-Q on the Mac) now asks first when this process serves the gateway
and agents are wired to it. The window comes up on a sheet
(quitask.js, lazy-loaded) that names each agent cut off, says closing the
window keeps magpie running (menu bar on a Mac, system tray elsewhere),
and offers Quit (focused), Disconnect and quit, and Cancel. Disconnect and
quit puts each agent back as its row's Disconnect does, then quits; a
disconnect that fails keeps magpie open, named in the status line. A
second Quit within a minute of the question quits, so a window that
can't show it traps nobody. With nothing wired, or another magpie serving
the gateway, Quit quits at once.

Not asked: a restart to update, Uninstall, a browser's page (magpie web),
logging out or shutting down, and the Dock's Quit (AppKit terminate:).

Found while checking the report:
- Closing the window on Windows hides it; magpie stays in the tray and
  the gateway keeps serving (tried for real, below). Not a bug.
- Autostart is off by default on Windows.
- The proxy / NO_PROXY in the report isn't magpie's to fix: the 502 is
  magpie's port with nothing behind it.

Strings in en, zh, zh-TW, ja and de (existing terms: Agent, 退出). No
native dialog, no <select>. TestCoreScriptBudget met without a raise:
app.js gains two lines, the sheet is outside the core, and driftFix's
comment block is cut to six lines (453 bytes back; main's core had grown
to 258 bytes over with these lines in).

Run, at this head (on origin/main 94f6f6092), on mote unless said:
- gofmt -l internal/gui: clean.
- go vet ./..., GOOS=windows go vet ./..., GOOS=linux go vet -tags
  nogui ./..., go build, GOOS=linux go build -tags nogui, GOOS=windows
  go build: ok.
- go test ./internal/gui -run 'Budget|Quit|CommandQ', and with -tags
  nogui -run 'Budget|Quit': ok (TestCommandQAsksFirst runs AppKit in a
  child process).
- quit-ask.test.cjs, old-webkit.test.cjs, i18n-keys.test.cjs in chromium
  and webkit, en/zh/zh-TW/ja/de: 60/60 ok.
- At the previous base (9d08993c8): go test ./internal/gui (whole
  package, GUI build): two reds. TestCoreScriptBudget (258 over, fixed
  above) and TestMainWindowCorners/macOS_26_SDK, red the same at
  9d08993c8 itself (#3949, macOS 27). The nogui run of the package was
  not reached.
- The whole GUI suite: started at 24fd4a250 on WSL (magpie-desktop),
  stopped about halfway (3,915 results) to rebase; 68 reds in 28 files.
  Those 28 files with quit-ask, old-webkit and i18n-keys at 9d08993c8+this
  on mote: 74 reds in 7 files, none in this change's files. The 7 alone
  at origin/main 9d08993c8: the same 6 files red (73), and
  gateway-caller-keys red at main when run alone (1 of 4). All filed:
  balance-fix #4682 #4683, key-replace #4126 #3476, agent-panel-name-width
  #4203 (red on macOS too, commented), context-map #3488, header-centre
  #3479, gateway-key-models #4616, gateway-caller-keys #3797.
- Not run: go test ./... in full (mote at load 140-160 with others' full
  suites, WSL with two GUI suites), and the GUI suite to its end.

Broken by hand, each red:
- api.go's ask skipped (false &&): TestWindowQuitAsksFirst "the window's
  Quit didn't ask (AskQuit never ran)".
- askingQuit without RemoveMenuItem: TestCommandQAsksFirst "the app menu
  still has the Quit role".
- quitNow as asked == 0 ||: TestQuitNow "connected, never asked: quitNow
  = true, want false".
- The return after a failed disconnect removed: quit-ask 5/25 chromium
  red (the failed-disconnect test in every language).
- app.js's ?ask=quit line removed: 20/25 webkit red.

Windows, for real (xiaomi-gpu, sandbox HOME/APPDATA under
C:\Users\yeton\quitask-sb, MAGPIE_ADDR 127.0.0.1:3471, MAGPIE_NO_ACCOUNTS,
a -H windowsgui build of this branch, launched in the desktop session):
`magpie codex fakeq/m1` wired the sandbox Codex to
http://127.0.0.1:3471/backend-api/codex; WM_CLOSE on the window left
the process alive with no visible window and the gateway listening.

Not tried:
- The tray's Quit and the sheet on real Windows: WebView2's CDP port
  never listened and UI Automation didn't find the tray icon, so the
  sheet was tried in Playwright only.
- A real Codex run against the sandbox gateway before and after a quit.
- The Dock's Quit (not covered by design).
2026-11-01 21:16:35 +08:00

131 lines
4.1 KiB
Go

package gui
import (
"encoding/json"
"net/http/httptest"
"testing"
"time"
"github.com/yetone/magpie/internal/agent"
)
// 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 := Handler(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 := Handler(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()
Handler(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)
}
}
}