Files
magpie/internal/gui/gatewaypause_test.go
T
yetone e8fbee60ea gui: pause the gateway without quitting magpie (me2seeks on X)
Disconnect and quit was the only way to stop the gateway, and it quits
magpie too. Pause does the same without the quit, from the tray's Pause
Gateway (Resume Gateway while paused), Settings › General › Gateway
(On/Paused, the app's only) and the Gateway page's Resume gateway, all
through POST /api/gateway/pause.

- Pause disconnects each connected agent as its row's Disconnect does
  (its own settings put back), keeps their ids in gateway-paused.json in
  the config folder, and stops the listener with requests in flight
  drained. An agent that can't be disconnected keeps the gateway serving,
  those before it connected again, and is named in the status line.
- While paused, serveGatewayLocked serves nothing: not the 15s watch, a
  port change nor a handover; a magpie started again stays paused. A
  pause file that can't be read counts as paused.
- Resume serves again and connects the agents the pause disconnected.
  A port held by something else keeps the pause and says what holds it.
  The Gateway page's Start/Restart, connecting an agent and picking one
  of magpie's models for it resume too.
- quitask's wired list and the pause share wiredOf.
- Strings in en, zh, zh-TW, ja, de. gui-shell.md documents it.

Run (mote was unreachable for the later runs; the Windows box instead):
- mote, before the rebase: go vet ./internal/gui; go test -run
  'TestGatewayPause|TestGatewayResumes|TestTray|TestGatewayFix|TestQuit'
  ./internal/gui: ok.
- at this tree on b46f23ffa: win-run go vet ./internal/gui ok; wsl-run
  go vet -tags nogui ./internal/gui ok; gofmt -l clean.
- win-run go test -count=1 ./internal/gui: the pause and tray tests ok;
  8 reds, each the same on origin/main d4b4a188e run alone on Windows
  and filed (#4115 %AppData% import rows, #5928 ~\ paths, #2107/#3649
  TestGatewayHeldByAContainer).
- wsl-run GUI (chromium + webkit): gateway-pause (5 languages x 1000/420),
  settings-general, gateway-takeover, i18n-keys, i18n-en-no-cjk: 51/52.
  The red is gateway-takeover webkit en's dark-mode case, red 1 of 2 on
  untouched origin/main too: #5942.
- rebased on 401187300: win-run go test -run 'Budget|TestGatewayPause|
  TestGatewayResumes|TestTray|TestQuit' ./internal/gui ok; wsl-run go vet
  -tags nogui ./internal/gui ok; gofmt clean. darwin vet not rerun after
  the rebase (mote refused SSH).

Broken by hand: serveGatewayLocked without its pausedNow check, and the
rollback's reconnect skipped: TestGatewayPauseAndResume ("served while
paused") and TestGatewayPauseRollsBack ("codex not connected again")
went red; green again restored.

Not tried: the real app's tray on a desktop, a real agent's config
round-trip (the Go tests fake the agents; Disconnect/connect are the
rows' own code), an agent wired from the CLI while the app is paused
(it doesn't resume), the whole GUI suite, the full Go suite (started after the tag).
2026-11-10 19:36:24 +08:00

224 lines
6.9 KiB
Go

package gui
import (
"errors"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"slices"
"strconv"
"strings"
"testing"
"github.com/yetone/magpie/internal/agent"
)
// pauseSandbox gives a test its own HOME, a gateway port of its own and
// fake agents: wired says which are connected, and the hooks' disconnects
// and connects change it and are counted. The gateway is served before
// the test and stopped after it.
func pauseSandbox(t *testing.T, ids ...string) (port int, wired map[string]bool, calls *[]string) {
t.Helper()
h := t.TempDir()
t.Setenv("HOME", h)
t.Setenv("USERPROFILE", h)
t.Setenv("APPDATA", filepath.Join(h, "AppData"))
t.Setenv("XDG_CONFIG_HOME", filepath.Join(h, ".config"))
t.Setenv("XDG_CACHE_HOME", filepath.Join(h, ".cache"))
port = freePort(t)
t.Setenv("MAGPIE_ADDR", "127.0.0.1:"+strconv.Itoa(port))
was := pausable.Load()
pausable.Store(true)
agents := map[string]*agent.Agent{}
wired = map[string]bool{}
for _, id := range ids {
agents[id] = &agent.Agent{ID: id, Name: strings.ToUpper(id)}
wired[id] = true
}
calls = &[]string{}
hooks := []any{pauseWired, pauseIsWired, pauseDisconnect, pauseConnect, pauseFind, onPaused}
pauseWired = func() []*agent.Agent {
var out []*agent.Agent
for _, id := range ids {
if wired[id] {
out = append(out, agents[id])
}
}
return out
}
pauseIsWired = func(a *agent.Agent) bool { return wired[a.ID] }
pauseDisconnect = func(a *agent.Agent) error {
*calls = append(*calls, "off "+a.ID)
wired[a.ID] = false
return nil
}
pauseConnect = func(a *agent.Agent) error {
*calls = append(*calls, "on "+a.ID)
wired[a.ID] = true
return nil
}
pauseFind = func(id string) (*agent.Agent, error) {
if a := agents[id]; a != nil {
return a, nil
}
return nil, errors.New("no agent " + id)
}
onPaused = nil
served.Store(nil)
t.Cleanup(func() {
gatewayMu.Lock()
stopServedLocked()
gatewayMu.Unlock()
pausable.Store(was)
pauseWired = hooks[0].(func() []*agent.Agent)
pauseIsWired = hooks[1].(func(*agent.Agent) bool)
pauseDisconnect = hooks[2].(func(*agent.Agent) error)
pauseConnect = hooks[3].(func(*agent.Agent) error)
pauseFind = hooks[4].(func(string) (*agent.Agent, error))
onPaused = hooks[5].(func())
})
gatewayMu.Lock()
out := startHere()
gatewayMu.Unlock()
if !out.OK || !answers(port) {
t.Fatalf("gateway not served: %+v", out)
}
return port, wired, calls
}
// me2seeks on X: quitting magpie with Disconnect and quit was the only way
// to stop its gateway. Pause disconnects every connected agent and stops
// the listener with the app still running; nothing serves it meanwhile;
// Resume serves it again and connects those agents again.
func TestGatewayPauseAndResume(t *testing.T) {
port, wired, calls := pauseSandbox(t, "codex", "claude")
told := 0
onPaused = func() { told++ }
out := pauseGateway()
if !out.Paused || out.Error != "" || !slices.Equal(out.Agents, []string{"codex", "claude"}) {
t.Fatalf("pause: %+v", out)
}
if wired["codex"] || wired["claude"] {
t.Fatalf("agents still connected: %v", wired)
}
if served.Load() != nil || answers(port) {
t.Fatal("the gateway still serves while paused")
}
if _, err := os.Stat(pausePath()); err != nil {
t.Fatalf("the pause isn't kept: %v", err)
}
if !providersState().Gateway.Paused || !settingsState().GatewayPaused {
t.Fatal("the pages aren't told of the pause")
}
if l := trayMenuLabels("en", "1", "", pausedNow()); l.pause != "Resume Gateway" {
t.Fatalf("tray reads %q", l.pause)
}
// nothing starts it meanwhile: the watch for a gateway gone, a port
// change, a handover
gatewayMu.Lock()
gw := serveGatewayLocked()
gatewayMu.Unlock()
if gw != nil || served.Load() != nil || answers(port) {
t.Fatal("served while paused")
}
// a second pause changes nothing
if again := pauseGateway(); !again.Paused || len(*calls) != 2 {
t.Fatalf("pause again: %+v, %v", again, *calls)
}
// the Gateway page's Start resumes
mux := http.NewServeMux()
gatewayFixRoutes(mux)
gatewayPauseRoutes(mux)
rec := httptest.NewRecorder()
mux.ServeHTTP(rec, httptest.NewRequest("POST", "/api/gateway/restart", strings.NewReader("{}")))
if !strings.Contains(rec.Body.String(), `"ok":true`) {
t.Fatalf("restart while paused: %s", rec.Body)
}
if pausedNow() || served.Load() == nil || !answers(port) {
t.Fatal("not resumed")
}
if !wired["codex"] || !wired["claude"] {
t.Fatalf("agents not connected again: %v", wired)
}
if !slices.Equal(*calls, []string{"off codex", "off claude", "on codex", "on claude"}) {
t.Fatalf("calls: %v", *calls)
}
if told != 2 {
t.Fatalf("the tray was told %d times", told)
}
// and the route pauses and resumes
for _, on := range []string{"true", "false"} {
rec := httptest.NewRecorder()
mux.ServeHTTP(rec, httptest.NewRequest("POST", "/api/gateway/pause", strings.NewReader(`{"on":`+on+`}`)))
if rec.Code != 200 || pausedNow() != (on == "true") || answers(port) == (on == "true") {
t.Fatalf("pause %s: %d %s", on, rec.Code, rec.Body)
}
}
}
// An agent that can't be disconnected leaves the gateway serving, and those
// disconnected before it are connected again: a pause never leaves an
// agent pointed at a gateway that isn't there.
func TestGatewayPauseRollsBack(t *testing.T) {
port, wired, calls := pauseSandbox(t, "codex", "claude")
off := pauseDisconnect
pauseDisconnect = func(a *agent.Agent) error {
if a.ID == "claude" {
return errors.New("config is read-only")
}
return off(a)
}
out := pauseGateway()
if out.Paused || out.Agent != "CLAUDE" || out.Error != "config is read-only" {
t.Fatalf("pause: %+v", out)
}
if pausedNow() || served.Load() == nil || !answers(port) {
t.Fatal("the gateway stopped though an agent stayed on it")
}
if !wired["codex"] || !slices.Equal(*calls, []string{"off codex", "on codex"}) {
t.Fatalf("codex not connected again: %v %v", wired, *calls)
}
}
// Connecting an agent while paused resumes: it would otherwise point at a
// gateway that isn't there.
func TestGatewayResumesOnWire(t *testing.T) {
port, wired, _ := pauseSandbox(t, "codex")
if out := pauseGateway(); !out.Paused {
t.Fatalf("pause: %+v", out)
}
a, _ := pauseFind("codex")
resumeOnWire(a) // not connected: stays paused
if !pausedNow() {
t.Fatal("resumed with no agent connected")
}
wired["codex"] = true
resumeOnWire(a)
if pausedNow() || !answers(port) {
t.Fatal("not resumed once an agent was connected")
}
}
// A pause file that can't be read is a pause, not a gateway to start; a
// process that isn't the app (magpie web, the CLI) is never paused.
func TestGatewayPauseFileUnreadable(t *testing.T) {
pauseSandbox(t)
if err := os.MkdirAll(filepath.Dir(pausePath()), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(pausePath(), []byte("{"), 0o644); err != nil {
t.Fatal(err)
}
if p, ids := gatewayPaused(); !p || ids != nil {
t.Fatalf("half-written file: %v %v", p, ids)
}
pausable.Store(false)
if pausedNow() {
t.Fatal("paused outside the app")
}
}