Files
magpie/internal/pluginrt/config_test.go
T
yetone 3ccbe6bc15 plugins: a runtime for built-in plugins, and Plugins › Built-in to switch them (P0)
Lxxyx on X (https://x.com/Lxxyx_/status/2109506752656150974): magpie has
too much in it. This is P0 of making magpie's parts plugins, as DeepSeek
Harness does: a runtime, plugins.yml, and the first five built-in plugins
the user can switch off.

- internal/pluginrt: a registry (Register from init), a manifest (API
  caret range, Requires/Provides, Essential, InUse, Apply, Parts,
  Defaults) and Reconcile: the plugins no longer wanted, with whatever
  needs a service they provided, are disposed newest first; the wanted
  ones are applied once their services are there. A plugin that fails or
  panics is failed and the rest run. TestPluginsImportOnlyCore keeps the
  boundary: nothing outside plugins/ imports one, none imports another.
- plugins.yml (appdir.Config()): default → preset (essentials | all) →
  the user's rows → in use. A new install is written Essentials, a magpie
  that ran here before (settings.RanBefore) All, so existing users keep
  everything. Writes are locked, whole and keep a .bak; a file that
  doesn't parse is an error, never empty, and is never written over.
- plugins/groups, library, otel, sessions, warmups. In P0 a plugin off
  is the window's alone: its page, sections and rows (data-feature) leave
  the window; the gateway, TUI and CLI serve the same, nothing is
  deleted. One in use stays on.
- The window: Plugins › Built-in, the first tab, a switch per plugin;
  GET/POST /api/plugins/builtin; boot.js says which are off before the
  first paint. CLI: magpie plugin builtin [on|off <id>], plugin preset,
  plugin dump [--json].
- docs/subsystems/plugin-runtime.md, gui-shell.md.

Run: go test ./internal/pluginrt/ ./internal/settings/ ./internal/gui/;
gofmt -l; go vet ./...; GOOS=linux go vet -tags nogui ./...; GOOS=windows
go vet ./.... GUI: builtins.test.cjs in Chromium and WebKit, 5 languages,
1100/800/420; list-sort, mobile-web, routing-keys-fold, routing-sessions
pass. By hand under a sandbox HOME: a new install gets essentials, an
existing one all, a broken plugins.yml is refused and kept, dump works.
Full suite results after the rebase are in the push notes.
2026-10-13 16:19:13 +08:00

128 lines
3.8 KiB
Go

package pluginrt
import (
"os"
"reflect"
"strings"
"testing"
)
func home(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
t.Setenv("MAGPIE_PORTABLE", "")
}
var three = []Manifest{
{ID: "core", Essential: true, Defaults: map[string]any{"n": 1, "deep": map[string]any{"a": 1, "b": 2}}},
{ID: "extra"},
{ID: "used", InUse: func() (int, error) { return 2, nil }},
}
func byID(cs []Composed) map[string]Composed {
m := map[string]Composed{}
for _, c := range cs {
m[c.ID] = c
}
return m
}
// The layers, field by field: the default, the preset, the user's rows,
// and in use over them all; each value says where it came from.
func TestCompose(t *testing.T) {
no := false
cs := byID(Compose(three, File{Preset: PresetEssentials, Plugins: []Row{
{ID: "core", Config: map[string]any{"deep": map[string]any{"b": 3}}},
{ID: "used", Enabled: &no},
}}))
if c := cs["core"]; !c.Enabled || c.Source != FromDefault {
t.Errorf("core: %+v", c)
}
if c := cs["core"]; !reflect.DeepEqual(c.Config, map[string]any{"n": 1, "deep": map[string]any{"a": 1, "b": 3}}) || c.Sources["deep"] != FromUser || c.Sources["n"] != FromDefault {
t.Errorf("core's config: %+v %+v", c.Config, c.Sources)
}
if c := cs["extra"]; c.Enabled || c.Source != FromPreset {
t.Errorf("extra under Essentials: %+v", c)
}
if c := cs["used"]; !c.Enabled || c.Chosen || c.Source != FromInUse || c.InUse != 2 {
t.Errorf("used, turned off: %+v", c)
}
// All, or no file at all: every one on
for _, f := range []File{{Preset: PresetAll}, {}} {
for _, c := range Compose(three, f) {
if !c.Enabled {
t.Errorf("%q: %s off", f.Preset, c.ID)
}
}
}
}
// A new install is written Essentials, one that ran before All; the file
// there is never written over by Settle; Set keeps the other rows and the
// preset, and the file it replaces is kept beside it.
func TestSettleAndSet(t *testing.T) {
home(t)
if wrote, err := Settle(true); err != nil || !wrote {
t.Fatalf("settle: %v %v", wrote, err)
}
if f, there, err := Load(); err != nil || !there || f.Preset != PresetEssentials {
t.Fatalf("after settle: %+v %v %v", f, there, err)
}
if wrote, _ := Settle(false); wrote {
t.Fatal("settled over a plugins.yml there")
}
if err := Set("extra", true); err != nil {
t.Fatal(err)
}
if err := Set("used", false); err != nil {
t.Fatal(err)
}
if err := Set("extra", false); err != nil {
t.Fatal(err)
}
f, _, err := Load()
if err != nil || f.Preset != PresetEssentials || len(f.Plugins) != 2 || *f.Plugins[0].Enabled || f.Plugins[0].ID != "extra" {
t.Fatalf("after sets: %+v %v", f, err)
}
if _, err := os.Stat(Path() + ".bak"); err != nil {
t.Fatalf("no .bak: %v", err)
}
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
if wrote, _ := Settle(false); !wrote {
t.Fatal("not settled")
}
if f, _, _ := Load(); f.Preset != PresetAll {
t.Fatalf("an existing user: %+v", f)
}
}
// A plugins.yml that doesn't parse is an error, never an empty file, and
// is never written over.
func TestBrokenFileIsKept(t *testing.T) {
home(t)
if err := os.MkdirAll(strings.TrimSuffix(Path(), "/plugins.yml"), 0o700); err != nil {
t.Fatal(err)
}
broken := []byte("plugins: [ {id: x\n")
if err := os.WriteFile(Path(), broken, 0o600); err != nil {
t.Fatal(err)
}
if _, _, err := Load(); err == nil {
t.Fatal("a broken file read")
}
if err := Set("x", true); err == nil {
t.Fatal("a broken file written")
}
if wrote, _ := Settle(true); wrote {
t.Fatal("a broken file settled over")
}
if b, _ := os.ReadFile(Path()); string(b) != string(broken) {
t.Fatalf("the broken file changed: %q", b)
}
if err := os.WriteFile(Path(), []byte("preset: slim\n"), 0o600); err != nil {
t.Fatal(err)
}
if _, _, err := Load(); err == nil || !strings.Contains(err.Error(), "slim") {
t.Fatalf("an unknown preset: %v", err)
}
}