Files
magpie/internal/cli/uninstall_cli_test.go
T
yetone 9d143eacab uninstall: install.sh --uninstall disconnects every agent first, a deleted magpie too
whimsical_wolf_15477 (Discord): after uninstalling magpie, Codex didn't go
back to its own config. `install.sh --uninstall` deleted the magpie command
and app and left every agent wired to a gateway that no longer runs: Codex
kept openai_base_url (or model_provider = "magpie" and model_catalog_json)
pointing at 127.0.0.1. A magpie deleted by hand undid nothing either, and
`--purge` refused with no magpie left to run.

- `magpie uninstall --keep-data` (uninstall.KeepData): every agent
  disconnected and its own values put back from stash.json, the service,
  autostart, shortcut, scheme and helpers removed, magpie's settings,
  providers and usage kept.
- install.sh --uninstall runs `magpie uninstall --yes --keep-data` (--purge:
  `--yes`) before deleting anything, and stops if it fails. With no magpie
  of its own (deleted by hand) but magpie's config left, it fetches the
  newest release into a temporary folder, checks its sha256, runs that, and
  deletes it after. An older magpie without --keep-data gets the newest one
  for this too. A magpie on PATH that the script didn't install is left to
  its own `magpie uninstall`, and said so.
- MAGPIE_UNINSTALL_SCRIPT=1: under the script magpie doesn't ask to delete
  a path that the script deletes next.
- docs (en/zh/ja start.html #uninstall) and READMEs: what the line does,
  that it works for a magpie already deleted, and Codex's lines by hand.

Run (WSL magpie-desktop; mote's disk is full, nothing run or deleted there):
- go test -tags nogui -run "Uninstall|Help|TestKeepData" ./internal/cli/
  ./internal/uninstall/: ok. node --test site/install.test.mjs: all pass.
- The new install tests against the old install.sh: 5 red. Broken by hand
  (--keep-data case removed, Data() back in KeepData): TestUninstallKeepData
  and TestKeepData red.
- Real e2e: magpie built from this tree, Codex wired in a sandbox HOME
  (not signed in, and ChatGPT signed in), magpie installed and deleted by
  hand; install.sh --uninstall with a local feed. config.toml's diff before
  wiring vs after is the [model_providers.magpie] table only (kept on
  purpose for threads started on magpie); model, effort, openai_base_url,
  model_provider and model_catalog_json are the user's again; settings kept.
- gofmt -l clean; go vet GOOS=windows/darwin/linux (nogui) for the two
  packages; sh -n and dash -n install.sh.

Not tried: macOS's zip path for a deleted magpie run for real (tests cover
it with fakes), Termux, Windows (no install.sh there; docs say download
magpie again and use Settings › Uninstall). The full internal/cli package
timed out at 10m under WSL load in providers_unreadable_cli_test (not
touched here); see the full suite record.
2026-11-05 12:52:46 +08:00

118 lines
5.2 KiB
Go

package cli
import (
"context"
"os"
"strconv"
"strings"
"testing"
"github.com/yetone/magpie/internal/daemon"
"github.com/yetone/magpie/internal/gateway"
"github.com/yetone/magpie/internal/uninstall"
)
func fakeUninstall(t *testing.T, served gateway.Served, service daemon.Status) *int {
t.Helper()
ran := 0
wasServed, wasStatus, wasRun, wasKeep := servedBy, serviceStatus, runUninstall, runKeepData
t.Cleanup(func() { servedBy, serviceStatus, runUninstall, runKeepData = wasServed, wasStatus, wasRun, wasKeep })
servedBy = func() gateway.Served { return served }
serviceStatus = func() (daemon.Status, error) { return service, nil }
runUninstall = func(context.Context) *uninstall.Report { ran++; return &uninstall.Report{} }
runKeepData = func(context.Context) *uninstall.Report { ran += 100; return &uninstall.Report{} }
return &ran
}
// install.sh --uninstall keeps magpie's settings, and took the gateway
// away with Codex still pointed at it (whimsical_wolf_15477 on Discord):
// --keep-data disconnects every agent and deletes none of magpie's files.
func TestUninstallKeepData(t *testing.T) {
ran := fakeUninstall(t, gateway.Served{}, daemon.Status{})
if err := uninstallCmd([]string{"--yes", "--keep-data"}); err != nil || *ran != 100 {
t.Fatalf("err %v, ran %d", err, *ran)
}
// without --yes and no terminal: nothing, and the refusal doesn't
// say the settings go
ran = fakeUninstall(t, gateway.Served{}, daemon.Status{})
err := uninstallCmd([]string{"--keep-data"})
if err == nil || strings.Contains(err.Error(), "deletes") || *ran != 0 {
t.Fatalf("err %v, ran %d", err, *ran)
}
}
// install.sh --uninstall deletes magpie after it runs, a downloaded copy
// in a temporary folder too: under the script, magpie doesn't tell the
// user to delete a path that is about to be gone.
func TestUninstallScriptLeavesExeToIt(t *testing.T) {
fakeUninstall(t, gateway.Served{}, daemon.Status{})
runKeepData = func(context.Context) *uninstall.Report {
return &uninstall.Report{Exe: "/tmp/tmp.x/magpie-cli-linux-amd64"}
}
out, err := stdoutOf(t, func() error { return uninstallCmd([]string{"--yes", "--keep-data"}) })
if err != nil || !strings.Contains(out, "/tmp/tmp.x/magpie-cli-linux-amd64") {
t.Fatalf("by hand: err %v, out %q", err, out)
}
t.Setenv("MAGPIE_UNINSTALL_SCRIPT", "1")
out, err = stdoutOf(t, func() error { return uninstallCmd([]string{"--yes", "--keep-data"}) })
if err != nil || strings.Contains(out, "magpie-cli-linux-amd64") || strings.Contains(out, "magpie is uninstalled") {
t.Fatalf("under the script: err %v, out %q", err, out)
}
}
// A magpie still serving that isn't the background service, which
// uninstalling removes first, would keep writing its files after its
// folders are gone: it is quit first, and nothing is changed.
func TestUninstallRefusesWhileMagpieRuns(t *testing.T) {
ran := fakeUninstall(t, gateway.Served{Running: true, Pid: 4242, Serves: "app"}, daemon.Status{})
err := uninstallCmd([]string{"--yes"})
if err == nil || !strings.Contains(err.Error(), "magpie's app is running (pid 4242)") || *ran != 0 {
t.Fatalf("err %v, ran %d", err, *ran)
}
// the background service itself is stopped and removed by uninstalling
ran = fakeUninstall(t, gateway.Served{Running: true, Pid: 4242, Serves: "serve"}, daemon.Status{Installed: true, Running: true, PID: 4242})
if err := uninstallCmd([]string{"--yes"}); err != nil || *ran != 1 {
t.Fatalf("err %v, ran %d", err, *ran)
}
// a magpie serve beside an installed service is another one
ran = fakeUninstall(t, gateway.Served{Running: true, Pid: 4242, Serves: "serve"}, daemon.Status{Installed: true, Running: true, PID: 99})
if err := uninstallCmd([]string{"--yes"}); err == nil || *ran != 0 {
t.Fatalf("err %v, ran %d", err, *ran)
}
}
// With no terminal to ask on, only --yes uninstalls; a word it doesn't
// know changes nothing.
func TestUninstallAsksFirst(t *testing.T) {
ran := fakeUninstall(t, gateway.Served{}, daemon.Status{})
if err := uninstallCmd(nil); err == nil || !strings.Contains(err.Error(), "--yes") || *ran != 0 {
t.Fatalf("err %v, ran %d", err, *ran)
}
if err := uninstallCmd([]string{"--help-me"}); err == nil || *ran != 0 {
t.Fatalf("err %v, ran %d", err, *ran)
}
if err := uninstallCmd([]string{"--yes"}); err != nil || *ran != 1 {
t.Fatalf("err %v, ran %d", err, *ran)
}
}
// --help prints its usage and uninstalls nothing; install.sh reads
// --keep-data in it to know this magpie can disconnect and keep its files.
func TestUninstallHelp(t *testing.T) {
ran := fakeUninstall(t, gateway.Served{}, daemon.Status{})
if !helpAsked([]string{"uninstall", "--help"}) || !strings.Contains(helpFor([]string{"uninstall"}), "magpie uninstall [--yes]") || !strings.Contains(helpFor([]string{"uninstall"}), "--keep-data") || *ran != 0 {
t.Fatal("no help for uninstall")
}
}
// --data-after with no process to wait for (0, a negative, this one)
// refuses at once: kill(0) asks the caller's own group, alive while it
// runs, and the files went only when the wait ran out.
func TestUninstallDataAfterNeedsAProcess(t *testing.T) {
for _, a := range []string{"0", "-1", "x", strconv.Itoa(os.Getpid())} {
if err := uninstallData([]string{a}); err == nil || !strings.Contains(err.Error(), "nothing was changed") {
t.Errorf("%s: %v", a, err)
}
}
}