Files
magpie/internal/gui/plugins_leftover_test.go
T
yetone ec97a4d7bb plugins: a dependency no plugin needs no longer fails another's install
CHENGJIAHEGOXIS on X (v0.1.4178, Windows) couldn't install GoAnyAPI from
Plugins: "agency-agents" failed with 从缓存复制到目标目录失败, and it was
on no plugin's row, so nothing could remove it.

Cause: every bun add/update/remove installs every dependency in
plugins/package.json again, and a failed `bun add` writes its package
there before exiting 1. agency-agents (a repository with no
package.json) was left by such an add; on Windows a file of its copy in
node_modules is held open, Bun fails "EBUSY: failed copying files from
cache to destination for package agency-agents", and so every later
install failed over a package nothing used.

Fix (internal/plugin/leftover.go, store.go):
- A package Bun says it couldn't install that no plugin is installed as
  (on the list, on or off, an add in flight, pi's agent) is taken out of
  package.json and node_modules and the command runs again: install()
  and runBun, so add, update, check and remove all get it.
- A failed install puts back the dependencies and bun.lock it found.
- An add whose own package went in while another listed plugin's
  failed succeeds; that plugin keeps its dependency.
- Remove and add's clean-ups drop the dependency by hand when
  bun remove fails.
- What is left from before is listed under Installed as "Not used by
  any plugin", each with a Remove (/api/plugins leftovers,
  POST /api/plugins/leftover; pmLeftovers in plugininstalled.js,
  imported when Plugins first draws), in every language.
- plugins.json there and unreadable: nothing is a leftover.
- Core script budget +51 for the one call left in plugins.js (reason in
  core_budget_test.go).

Run:
- mote-run: go vet ./..., GOOS=linux go vet -tags nogui ./...,
  GOOS=windows go vet ./...: ok. go test ./internal/plugin (169s) and
  ./internal/provider (148s): ok.
- mica-run go test -count=1 ./internal/gui with the leftovers drawing
  moved into plugininstalled.js: ok (133s); main since touches none of
  internal/gui's core scripts, internal/plugin or plugins.go.
- win-run go test -run 'Leftover|FailedAdd|AnotherPlugin|RemoveTakes|
  BunFailed' ./internal/plugin on xiaomi-gpu: ok.
- Reproduced the EBUSY with Bun 1.4.2 on xiaomi-gpu (a file in
  node_modules/agency-agents held open, then bun add another package).
- Red on the old store.go: TestLeftoverDoesNotFailAnotherAdd,
  TestFailedAddLeavesNoDependency, TestAnotherPluginsFailureDoesNotFailAdd,
  TestRemoveTakesDependencyOutWhenBunFails.
- GUI: plugin-leftover.test.cjs 42/42 (chromium + webkit, en zh zh-TW
  ja de, 980/420px, with and without a plugin); old plugins.js: 21/21
  chromium red.
- mote-run, rebased on fdf42299e: plugin-leftover + i18n-keys 46/46;
  every other plugin*.test.cjs, old-webkit and i18n-en-no-cjk: 255 ok,
  reds only in old-webkit (#6030, #5919), plugin-move-unserved (#4392),
  plugin-option-placement (#6559) and plugin-page-foreign-error
  (#6117), each red the same on a clean origin/main fdf42299e checkout.

Not run: the full go test ./... and the whole GUI suite (mote over
its cores, mica running another session's full suite; one per machine); a real Windows
install through the magpie app.
2026-11-15 03:07:38 +08:00

54 lines
1.9 KiB
Go

package gui
import (
"context"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
"github.com/yetone/magpie/internal/plugin"
"github.com/yetone/magpie/internal/settings"
)
// What a failed install left in plugins/ that no plugin is installed as
// reaches the page (leftovers), and its Remove (/api/plugins/leftover)
// asks for that name: nothing showed agency-agents, so nothing could take
// it out (CHENGJIAHEGOXIS on X).
func TestPluginLeftoversReachThePage(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())
if err := os.MkdirAll(plugin.Dir(), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(plugin.Dir(), "package.json"), []byte(`{"dependencies":{"agency-agents":"github:msitarzewski/agency-agents"}}`), 0o644); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(settings.Dir(), "plugins.json"), []byte(`{"plugins":[]}`), 0o600); err != nil {
t.Fatal(err)
}
old := dropLeftover
t.Cleanup(func() { dropLeftover = old })
var asked []string
dropLeftover = func(ctx context.Context, name string) error { asked = append(asked, name); return nil }
s := pluginsState(context.Background(), nil)
if len(s.Leftovers) != 1 || s.Leftovers[0].Name != "agency-agents" || s.Leftovers[0].Spec != "github:msitarzewski/agency-agents" {
t.Fatalf("leftovers = %+v", s.Leftovers)
}
mux := http.NewServeMux()
pluginRoutes(mux, nil)
rec := httptest.NewRecorder()
mux.ServeHTTP(rec, httptest.NewRequest("POST", "/api/plugins/leftover", strings.NewReader(`{"spec":"agency-agents"}`)))
if rec.Code != 200 || strings.Join(asked, ",") != "agency-agents" {
t.Fatalf("POST leftover: %d %s, asked %v", rec.Code, rec.Body, asked)
}
}