Files
magpie/internal/gui/plugins_outlive_test.go
T
yetone c55395df9e gui: a plugin install goes on when the proxy in front of magpie web gives up (#2032, precisionz)
precisionz installed Zed from magpie web on a phone; the page spun for a
while and then showed Cloudflare's own error page ("<!DOCTYPE html>
<!--[if lt IE 7]> <html class="no-js ie…"). A tunnel or proxy in front of
magpie web stops waiting after its own limit (Cloudflare: 100 seconds,
then 524), closes the connection, and /api/plugins/{op} ran bun under the
request's context, so the install was cancelled halfway.

/api/plugins/{op} now runs under context.WithoutCancel(r.Context()) with
the same 5-minute cap, as moveContext already does for a move. add,
remove, update and upgrade go through test seams (addPlugin, ...).

Verified:
- TestPluginOpsOutliveRequest (new) cancels each request before serving
  it. With r.Context() put back it fails: "got [add: stopped with the
  page remove: stopped with the page update: stopped with the page
  upgrade: stopped with the page]". With the fix it passes.
- HOME=<temp> go test -tags nogui ./internal/gui/: ok (107s).
- gofmt -l clean; GOOS=linux and GOOS=windows go vet -tags nogui
  ./internal/gui/: clean.
Not tried: a real Cloudflare tunnel in front of magpie web.
2026-10-14 04:22:19 +08:00

58 lines
2.2 KiB
Go

package gui
import (
"context"
"net/http"
"net/http/httptest"
"path/filepath"
"strings"
"testing"
"github.com/yetone/magpie/internal/plugin"
)
// An install goes on though the page's connection goes: on magpie web
// behind Cloudflare the tunnel gives up after 100 seconds and answers the
// page with its own 524 page (precisionz installing Zed, #2032), and bun
// stopped halfway leaves plugins/ half-written. So do a remove, an update
// and an upgrade.
func TestPluginOpsOutliveRequest(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config"))
t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache"))
t.Setenv("MAGPIE_PLUGIN_MARKET", "off")
t.Setenv("PATH", t.TempDir())
oldAdd, oldRemove, oldUpdate, oldUpgrade := addPlugin, removePlugin, updatePlugins, upgradePlugin
t.Cleanup(func() {
addPlugin, removePlugin, updatePlugins, upgradePlugin = oldAdd, oldRemove, oldUpdate, oldUpgrade
})
var got []string
check := func(op string, ctx context.Context) {
if _, deadline := ctx.Deadline(); ctx.Err() != nil || !deadline {
got = append(got, op+": stopped with the page")
} else {
got = append(got, op+": going")
}
}
addPlugin = func(ctx context.Context, spec string) (plugin.Entry, error) {
check("add", ctx)
return plugin.Entry{}, nil
}
removePlugin = func(ctx context.Context, spec string) error { check("remove", ctx); return nil }
updatePlugins = func(ctx context.Context) error { check("update", ctx); return nil }
upgradePlugin = func(ctx context.Context, name string) error { check("upgrade", ctx); return nil }
mux := http.NewServeMux()
pluginRoutes(mux, nil)
for _, op := range []string{"add", "remove", "update", "upgrade"} {
ctx, cancel := context.WithCancel(context.Background())
cancel() // the tunnel gave up waiting
r := httptest.NewRequest("POST", "/api/plugins/"+op, strings.NewReader(`{"spec":"@magpie-community/opencode-zed-auth"}`)).WithContext(ctx)
mux.ServeHTTP(httptest.NewRecorder(), r)
}
if want := "add: going,remove: going,update: going,upgrade: going"; strings.Join(got, ",") != want {
t.Fatalf("got %v, want %s", got, want)
}
}