Files
magpie/internal/cli/healthcheck_cli_test.go
T
yetone 7d5db0f29a cli: a command that ends ends its plugin hosts, and one that doesn't serve starts none (#7146)
athemeroy (WangYeruo on X) runs `magpie serve` as a LaunchAgent with the
magpie-google-ai-studio browser plugin and 11 accounts. Each
`magpie --version`, `version`, `licenses` or `group <id>` run beside it
left a second magpie-bun host with PPID 1, holding the Chrome profiles
the gateway's host wanted ("Chrome exited before starting WebView").
Their analysis was right on each count:

- plugin.Cached starts a background list of the plugins' providers
  whenever this process hasn't listed them (provGood is in memory), so
  every short command reaching provider.Catalog (startUp's RenameLegacy,
  groups, models, ...) started a host.
- Main ended through os.Exit and never stopped it. A host whose stdin
  closes leaves only once nothing is pending, and a plugin holding a
  browser keeps Bun up meanwhile, so the host outlived magpie, adopted
  by init. SIGTERM (launchd stopping serve) orphaned serve's own host
  the same way.
- --version and licenses were answered after startUp.

Now:
- Cached asks the plugins in the background only when
  plugin.ListInBackground is on; run sets it from servesPages (app,
  web, serve, TUI). Other commands read the list on disk.
- Main ends through exit(), which runs plugin.EndHosts first, on
  success and error; die does too on SIGINT/SIGTERM/SIGHUP, bounded by
  hostsOnSignal (20s, past the 15s a renewing host gets).
- provider test returns exitError instead of os.Exit(1).
- -v/--version/version and licenses answer before any start-up work.

Audit of the os.Exit calls in internal/cli: data_cli.go:135 and the
CheckEnv exit in Main run before anything can start a host;
claudeLetGo already called EndHosts.

Tests (red on the old code, the non-test sources reverted to
14b6b9555, green with the fix):
- TestOneShotCommandsStartNoPluginHost: groups, group fakeco, models,
  providers, accounts each started a host (5 reds); version, --version,
  -v, licenses and agents start none; Cached in a serving magpie still
  lists.
- TestExitEndsPluginHosts: a real host whose stuck usage hook holds it
  up (new STUCK_USAGE_HOLD, STUCK_PID in testdata/stuck), the magpie
  ended by `version`, by an error and by SIGTERM: all three left the
  host at PPID 1 on the old code, none now. 10 of 10 on macOS, 3 of 3
  on Linux (WSL), version/error on Windows (no SIGTERM there).
- TestSessionCommandsSkipStartUp: version/--version/-v/licenses don't
  run startUp (red on the old code for version). Three tests that used
  `version` to reach startUp/makeDirs use `groups`.

Real use, sandbox HOME on mote, a plugin whose provider list takes 90s
holding a timer, signed in with a key, `magpie serve` running:
- old binary: groups, group holdco and models each left a magpie-bun
  at PPID 1; after SIGTERM to serve, serve's host was at PPID 1 too
  (4 left).
- new binary: only serve's host, and none left after SIGTERM. The
  commands take 0.02-0.15s.

Run at the pushed head:
- gofmt -l: nothing. go vet ./..., GOOS=linux go vet -tags nogui ./...,
  GOOS=windows go vet ./..., go build: ok (mote).
- go test -count=1 -timeout 45m ./... on mote: 102 packages ok, two
  red, each red the same at the base 14b6b9555 (-count=3/5 there):
  internal/cli TestHealthcheckFindsTheRunningGatewayWithoutMagpieAddr,
  TestUpdateRestartsTheGatewayItFindsWithoutMagpieAddr and
  TestUpdateFindsAMagpieThatWroteNoRecord (#6471, #6223: an ssh
  session's forward holds 127.0.0.1:3425 on mote), and internal/gateway
  TestOpenCodeGoOwnFreeModelGetsNothingOfOpenCodes (#7117).
- Rebased on 598bc4534 (it changes internal/cli's update code), at
  the pushed head: go test ./internal/cli ./internal/plugin on mote:
  plugin ok; cli ok but for the same three #6471 reds.

Not tried: the reporter's real Google AI Studio plugin with Chrome, and
a real LaunchAgent stop (launchctl); the repro sends serve SIGTERM as
launchd does.
2026-11-19 10:38:25 +08:00

188 lines
6.4 KiB
Go

package cli
import (
"encoding/json"
"fmt"
"net"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
"github.com/yetone/magpie/internal/gateway"
)
// magpie healthcheck, a container's HEALTHCHECK, passes only while a
// magpie gateway answers at MAGPIE_ADDR.
func TestHealthcheck(t *testing.T) {
t.Setenv("HOME", t.TempDir())
t.Setenv("USERPROFILE", os.Getenv("HOME"))
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
magpie := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`{"name":"magpie","version":"dev"}`))
}))
defer magpie.Close()
t.Setenv("MAGPIE_ADDR", strings.TrimPrefix(magpie.URL, "http://"))
if err := run([]string{"healthcheck"}); err != nil {
t.Fatal("magpie answering:", err)
}
other := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`{"name":"nginx"}`))
}))
defer other.Close()
t.Setenv("MAGPIE_ADDR", strings.TrimPrefix(other.URL, "http://"))
if err := run([]string{"healthcheck"}); err == nil {
t.Error("something else answering passed")
}
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
addr := ln.Addr().String()
ln.Close()
t.Setenv("MAGPIE_ADDR", addr)
if err := run([]string{"healthcheck"}); err == nil {
t.Error("nothing listening passed")
}
}
// The Docker image sets HOME and the XDG folders inside its volume, which
// a volume an older image made doesn't have yet: magpie makes them.
func TestMakeDirs(t *testing.T) {
vol := t.TempDir()
for k, d := range map[string]string{"HOME": "home", "XDG_CONFIG_HOME": "config", "XDG_CACHE_HOME": "cache", "XDG_DATA_HOME": "data", "XDG_STATE_HOME": "state"} {
t.Setenv(k, filepath.Join(vol, d))
}
t.Setenv("MAGPIE_ADDR", "127.0.0.1:1")
if err := run([]string{"groups"}); err != nil {
t.Fatal(err)
}
for _, d := range []string{"home", "config", "cache", "data", "state"} {
if fi, err := os.Stat(filepath.Join(vol, d)); err != nil || !fi.IsDir() {
t.Errorf("%s not made: %v", d, err)
}
}
}
// writeListening is a running magpie's note of where it listens
// (gateway.ListeningFile), for a command run in another shell.
func writeListening(t *testing.T, addr string, pid int) {
t.Helper()
dir := filepath.Join(os.Getenv("XDG_CONFIG_HOME"), "magpie")
if err := os.MkdirAll(dir, 0o700); err != nil {
t.Fatal(err)
}
b := fmt.Sprintf(`{"addr":%q,"pid":%d,"serves":"web","version":"v9.9.9","started":"2026-11-11T00:00:00Z"}`, addr, pid)
if err := os.WriteFile(filepath.Join(dir, "gateway.json"), []byte(b), 0o600); err != nil {
t.Fatal(err)
}
}
// fakeServing answers as the magpie with pid, at a port that isn't Settings'.
func fakeServing(t *testing.T, pid int) string {
t.Helper()
t.Setenv("HOME", t.TempDir())
t.Setenv("USERPROFILE", os.Getenv("HOME"))
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
t.Setenv("MAGPIE_ADDR", "")
m := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
w.Write([]byte(`{"state":"restarting","version":"v9.9.9"}`))
return
}
fmt.Fprintf(w, `{"name":"magpie","version":"v9.9.9","pid":%d,"serves":"web"}`, pid)
}))
t.Cleanup(m.Close)
return strings.TrimPrefix(m.URL, "http://")
}
// ljlwlcz on X: a service started with MAGPIE_ADDR=0.0.0.0:10100, and
// `magpie healthcheck` run in a shell without it looked at 3425 and
// exited 1. The running magpie's own note says where it listens.
func TestHealthcheckFindsTheRunningGatewayWithoutMagpieAddr(t *testing.T) {
addr := fakeServing(t, 4242)
writeListening(t, addr, 4242)
if err := run([]string{"healthcheck"}); err != nil {
t.Fatal("a gateway that wrote down where it listens:", err)
}
// a note whose process isn't the one answering there (a stale file, a
// port another program took) is not believed
writeListening(t, addr, 777)
if err := run([]string{"healthcheck"}); err == nil {
t.Error("a note naming another pid than the one answering passed")
}
// --addr is the caller's word, over the note
if err := run([]string{"healthcheck", "--addr", addr}); err != nil {
t.Error("--addr:", err)
}
if err := run([]string{"healthcheck", "--addr=" + addr}); err != nil {
t.Error("--addr=:", err)
}
}
// A flag or a value healthcheck doesn't know is refused, not taken as an
// address (LESSONS: a command never takes a flag as data).
func TestHealthcheckRefusesWhatItDoesNotKnow(t *testing.T) {
fakeServing(t, 1)
for _, args := range [][]string{
{"healthcheck", "--bogus"}, {"healthcheck", "x"}, {"healthcheck", "--addr"},
{"healthcheck", "--addr", "nonsense"}, {"healthcheck", "--addr", "127.0.0.1:0"}, {"healthcheck", "--addr", "127.0.0.1:99999"},
} {
if err := run(args); err == nil || strings.Contains(err.Error(), "no gateway at") {
t.Errorf("magpie %v: %v", args, err)
}
}
}
// `magpie update` asked the address it knew and said "magpie isn't running"
// while the service served on another one: its restart now goes where the
// running magpie says it listens.
func TestUpdateRestartsTheGatewayItFindsWithoutMagpieAddr(t *testing.T) {
addr := fakeServing(t, 4242)
writeListening(t, addr, 4242)
if got := restartRunning(gateway.ClientURL(), "v9.9.10"); !strings.Contains(got, "restarts into v9.9.10") {
t.Errorf("update found no running magpie: %q", got)
}
if got := restartRunning(gateway.URL(), "v9.9.10"); !strings.Contains(got, "isn't running") {
t.Errorf("the address Settings has is not where the fake serves: %q", got)
}
}
// magpie status says the role, pid and address of the magpie that serves
// (a watchdog can't tell magpie.exe tray from magpie.exe mcp image by name).
func TestStatusJSON(t *testing.T) {
addr := fakeServing(t, 4242)
writeListening(t, addr, 4242)
out, err := runOut(t, "status", "--json")
if err != nil {
t.Fatal(err)
}
var st struct {
Running bool
Role string
Pid int
Version string
Addr string
}
if err := json.Unmarshal([]byte(out), &st); err != nil {
t.Fatalf("%v: %q", err, out)
}
if !st.Running || st.Role != "web" || st.Pid != 4242 || st.Version != "v9.9.9" || st.Addr != addr {
t.Errorf("status: %+v (want %s)", st, addr)
}
// none running: said so, as JSON
writeListening(t, "127.0.0.1:1", 4242)
t.Setenv("MAGPIE_ADDR", "127.0.0.1:1")
out, err = runOut(t, "status", "--json")
if err != nil || !strings.Contains(out, `"running": false`) {
t.Errorf("nothing serving: %v %q", err, out)
}
if _, err := runOut(t, "status", "--nope"); err == nil {
t.Error("an unknown flag was taken")
}
}