Files
magpie/internal/plugin/page_test.go
T
yetone b9f790d37b plugins: panels in magpie's pages, a page that replaces a built-in one, titles in the window's language, and a look that matches
P5 of the plugin runtime (docs/subsystems/plugin-runtime.md):

- Panels (magpie.panels): a plugin draws a frame into a slot of a
  built-in page (a provider's editor, Settings, Usage); magpie.context
  says where it is, and the frame is as tall as what it draws.
- A page can replace a built-in one (magpie.page.replaces); show() opens
  it in that page's place, and the sidebar row goes to it.
- page.titles and a panel's titles name it in the window's language
  ({"zh": …}); a malformed map is dropped, the title kept
  (TestWindowTitles).
- main's #2501 already names wails:// in the policy of a page in the
  desktop window (through ownWindow), which is why the usage page drew as
  bare HTML from v0.1.1259. TestPluginPageLoadsItsFilesInTheWindow loads
  a page through ownWindow in a real WKWebView, which Playwright on http
  can't; the subsystem page says how.
- The bridge adds magpie's look for what a page leaves unstyled, every
  rule in :where() so any rule of the page's wins, and the theme's
  colours as --magpie-* variables.

One commit: the panels, the replacing pages and the titles share hunks
in page.go, pluginpage.go, pluginpages.js and the docs, and couldn't be
split cleanly.

Run:
- Full: this series (P5, the Plugins dot, the probe test, P6, and a
  built-ins-on commit since dropped for main's own 5a2414acd) was run
  together, at load 200-660.
  At the pushed tree, 88dfab2d3 on origin/main 5380114da (rebased
  from 210bc2b77, no conflicts; its GUI assets and tests are the same
  bytes as the tree the GUI runs below used, diff -rq): gofmt clean,
  vet darwin/linux nogui/windows ok, TestCoreScriptBudget ok
  (1,446,845 of 1,447,000). go test -timeout 45m ./... at load
  193-278: 62 packages ok, red in TestMainWindowCorners (#1936) and
  TestPluginStreamLargeReplies JSON_fast (client timeout; alone 1 of
  3 red here, 0 of 3 on 5380114da side by side at load ~280; the
  series doesn't touch the stream path; #2551 reopened with the
  output, taken). Two earlier runs at this tree are discarded: one
  stopped when the disk filled, one when another session's go clean
  -cache emptied the build cache mid-run. The whole GUI suite at
  this tree was not finished (the disk filled); the files the series
  reaches ran (1318 tests, 124 red), and the 39 red files ran again
  at concurrency 1 on both trees: here 148 ok 120 red, origin/main
  135 ok 127 red. Both sides' reds are page.goto/launch timeouts
  under load (66 here, 71 there) and the same assertions:
  title-bar-tools' and settings-proxy-auth's "access control checks"
  on a cut-off /api/providers read (#2740, taken next), sidebar's
  "the list unrolls" and WebKit "#prefs pressed darkens" (#2684;
  alone, 1 red here, 3 on main), side-toggle (#2732); plugin-move
  red on main only. Six files again one by one on both trees:
  plugin-actions red 1 of 6 here, a 5s goto of ?view=plugins at load
  260; plugin-clash red on both; plugin-own-icon, plugin-gateway,
  plugin-checkin ok on both; settings-proxy-auth ok here, 3 red on
  main.
  At 976c9893a (on origin/main 6f950653f): go test -timeout 45m ./...
  red only in TestMainWindowCorners (#1936, the window's process
  killed at its deadline) and TestSetAnswersWhileDevinAndProvidersHang
  (#1375, 3 of 3 alone). The whole GUI suite, both engines: 10834
  tests, 237 red in 73 files, 184 of them a 180s browser launch. Those
  73 files run again at concurrency 2: 52 green, among them every
  plugin-* and agent-* file and plugin-actions; the 21 still red were
  run at origin/main f191d6423 and are red there too: codex-predictions
  (#2624), lightweight (#2638), segs-glide (#2683), usage-agents-hide
  (#2276), provider-advanced-fold (#2552), routing-bodies (#2559),
  routing-model-rest and rest-try-now (#2115, #2564), routing-keys-fold
  (#1612), routing-wb-restricted (#1905), routing-request-metrics
  (#2571), quota-count-wrap and quota-name-translated (#2228), s3-sync
  (#2448), usage-checkin (#2549), usage-ledger-abort (#2700),
  tencent-access-key (#2733), usage-range (#2734); side-toggle's WebKit
  motion red 1 of 3 on main, 0 of 3 here (#2732); usage-fold green alone.
  Rebased onto origin/main 717b69f78 (60725faa1, no conflicts): gofmt
  clean, vet darwin/linux nogui/windows ok; go test -timeout 45m ./...
  red in TestSetAnswersWhileDevinAndProvidersHang (#1375) and agent and
  gateway at the 45m alarm with tests still moving at load 400+, both ok
  alone (1173s, 1167s); 105 GUI files the series reaches (plugin*,
  sidebar*, side-*, settings*, title*, header*, agent-*): 1322 tests,
  23 red, run alone 214 of 218 ok, the 4 left in sidebar and
  sidebar-overlay-scrollbar (WebKit), red on 717b69f78 too (#2684,
  #1863). The whole GUI suite was not run again after the rebase.
- plugin-panels' tall panel (body{margin:0}) measured 220, not 180,
  after the look above gave an unstyled body 20px of padding; the
  fixture now sets padding:0 too, as a page that wants none would.
  plugin-panels ok in chromium and webkit after.
- gofmt -l clean; go vet darwin, linux (-tags nogui) and windows ok.
- GUI: plugin-page and plugin-panels in chromium and webkit, en zh zh-TW
  ja de, wide and narrow, ok.
- Broken by hand, each red then green: titled() in pluginpages.js
  (the "draws in its frame" test), the wails:// scheme in pagePolicy
  (the WKWebView test, before the rebase onto #2501), titles' parsing
  (TestWindowTitles).
- Real use: the published @magpie-community/page-usage 0.1.1 in a
  sandbox magpie window, its tiles styled.
2026-10-18 07:13:09 +08:00

176 lines
6.7 KiB
Go

package plugin
import (
"os"
"path/filepath"
"slices"
"testing"
)
// A plugin's page (magpie.page) is a file inside the plugin's folder: one
// that names a path out of it, by "..", an absolute path or a symbolic
// link, is no page. Its reads are those magpie has, and a plugin that is
// only a page is nothing for the host to load, nor "not a plugin".
func TestWindowPageStaysInside(t *testing.T) {
root := t.TempDir()
outside := filepath.Join(root, "secret.html")
if err := os.WriteFile(outside, []byte("<p>secret</p>"), 0o644); err != nil {
t.Fatal(err)
}
mk := func(name, pkg string) string {
t.Helper()
d := filepath.Join(root, name)
if err := os.MkdirAll(filepath.Join(d, "page"), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(d, "page", "index.html"), []byte("<p>hi</p>"), 0o644); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(d, "package.json"), []byte(pkg), 0o644); err != nil {
t.Fatal(err)
}
return d
}
good := mk("good", `{"name":"good","magpie":{"page":{"title":"Good","file":"page/index.html","icon":"page/index.html"},"permissions":["usage:summary","secrets:all","usage:summary"]}}`)
w, only := Window(good)
if w == nil || w.Page == nil || !only || w.Page.Title != "Good" || w.Page.Icon != "index.html" {
t.Fatalf("good: %+v only=%v", w, only)
}
p := w.Page
if !slices.Equal(w.Reads, []string{"usage:summary"}) {
t.Errorf("reads %v, want only the one magpie has, once", w.Reads)
}
if !InMagpieOnly(good) || notPlugin(good) != nil {
t.Errorf("a page-only plugin: InMagpieOnly %v, notPlugin %v", InMagpieOnly(good), notPlugin(good))
}
if _, only := Window(mk("main", `{"name":"m","main":"index.js","magpie":{"page":{"file":"page/index.html"}}}`)); only {
t.Error("a plugin with a main is more than its page")
}
link := mk("link", `{"name":"l","magpie":{"page":{"file":"page/out.html"}}}`)
if err := os.Symlink(outside, filepath.Join(link, "page", "out.html")); err != nil {
t.Fatal(err)
}
for name, pkg := range map[string]string{
"dotdot": `{"name":"d","magpie":{"page":{"file":"../secret.html"}}}`,
"abs": `{"name":"a","magpie":{"page":{"file":"` + filepath.ToSlash(outside) + `"}}}`,
"notml": `{"name":"n","magpie":{"page":{"file":"package.json"}}}`,
"none": `{"name":"x","magpie":{"page":{"file":"page/missing.html"}}}`,
} {
if w, _ := Window(mk(name, pkg)); w != nil {
t.Errorf("%s: %+v is a page", name, w)
}
}
if w, _ := Window(link); w != nil {
t.Errorf("a symbolic link out of the folder: %+v is a page", w)
}
if f, ok := FileIn(p.Dir, "../package.json"); ok {
t.Errorf("FileIn served %s", f)
}
if f, ok := FileIn(p.Dir, "index.html"); !ok || filepath.Base(f) != "index.html" {
t.Errorf("FileIn(index.html) = %s %v", f, ok)
}
}
// A plugin's panels (magpie.panels) go only where magpie has a slot, each
// an .html inside the plugin's folder, at most maxPanels of them; a page
// replaces only a built-in page that can be replaced. A plugin that is
// only panels is a window plugin all the same: in magpie only, and a
// plugin.
func TestWindowPanels(t *testing.T) {
root := t.TempDir()
if err := os.WriteFile(filepath.Join(root, "secret.html"), []byte("<p>secret</p>"), 0o644); err != nil {
t.Fatal(err)
}
d := filepath.Join(root, "p")
if err := os.MkdirAll(filepath.Join(d, "ui"), 0o755); err != nil {
t.Fatal(err)
}
for _, f := range []string{"ui/usage.html", "ui/prov.html", "ui/page.html"} {
if err := os.WriteFile(filepath.Join(d, filepath.FromSlash(f)), []byte("<p>hi</p>"), 0o644); err != nil {
t.Fatal(err)
}
}
write := func(pkg string) {
t.Helper()
if err := os.WriteFile(filepath.Join(d, "package.json"), []byte(pkg), 0o644); err != nil {
t.Fatal(err)
}
}
write(`{"name":"p","magpie":{"panels":[
{"slot":"usage","file":"ui/usage.html","title":"Spend"},
{"slot":"provider","file":"ui/prov.html"},
{"slot":"nowhere","file":"ui/usage.html"},
{"slot":"usage","file":"../secret.html"},
{"slot":"usage","file":"ui/missing.html"},
{"slot":"agents","file":"package.json"}
],"permissions":["providers:list"]}}`)
w, only := Window(d)
if w == nil || !only || w.Page != nil {
t.Fatalf("panels only: %+v only=%v", w, only)
}
var got []string
for _, p := range w.Panels {
got = append(got, p.Slot+":"+p.Title+":"+filepath.Base(p.File))
}
if want := []string{"usage:Spend:usage.html", "provider::prov.html"}; !slices.Equal(got, want) {
t.Errorf("panels %v, want %v", got, want)
}
if !slices.Equal(w.Reads, []string{"providers:list"}) {
t.Errorf("reads %v", w.Reads)
}
if !InMagpieOnly(d) || notPlugin(d) != nil {
t.Errorf("a panels-only plugin: InMagpieOnly %v, notPlugin %v", InMagpieOnly(d), notPlugin(d))
}
many := `{"slot":"usage","file":"ui/usage.html"}`
for range maxPanels {
many += `,{"slot":"usage","file":"ui/usage.html"}`
}
write(`{"name":"p","magpie":{"panels":[` + many + `]}}`)
if w, _ := Window(d); w == nil || len(w.Panels) != maxPanels {
t.Errorf("%+v kept, want %d panels at most", w, maxPanels)
}
for replaces, want := range map[string]string{"usage": "usage", "sessions": "sessions", "agents": "", "settings": "", "plugins": "", "": ""} {
write(`{"name":"p","magpie":{"page":{"file":"ui/page.html","replaces":"` + replaces + `"}}}`)
w, _ := Window(d)
if w == nil || w.Page == nil || w.Page.Replaces != want {
t.Errorf("replaces %q: %+v, want %q", replaces, w, want)
}
}
}
// A page's and a panel's titles by language (magpie.page.titles) are kept
// trimmed, without the empty ones; titles that aren't an object of strings
// leave the page as it was, with its title.
func TestWindowTitles(t *testing.T) {
d := t.TempDir()
if err := os.WriteFile(filepath.Join(d, "index.html"), []byte("<p>hi</p>"), 0o644); err != nil {
t.Fatal(err)
}
write := func(titles string) *WindowSpec {
t.Helper()
pkg := `{"name":"p","magpie":{"page":{"title":"Glance","file":"index.html","titles":` + titles + `},
"panels":[{"slot":"usage","file":"index.html","title":"Spend","titles":` + titles + `}]}}`
if err := os.WriteFile(filepath.Join(d, "package.json"), []byte(pkg), 0o644); err != nil {
t.Fatal(err)
}
w, _ := Window(d)
if w == nil || w.Page == nil || len(w.Panels) != 1 || w.Page.Title != "Glance" || w.Panels[0].Title != "Spend" {
t.Fatalf("titles %s: %+v", titles, w)
}
return w
}
w := write(`{" zh ":" 速览 ","ja":"","de":7,"":"x"}`)
for _, m := range []map[string]string{w.Page.Titles, w.Panels[0].Titles} {
if len(m) != 1 || m["zh"] != "速览" {
t.Errorf("titles %v, want only zh 速览", m)
}
}
for _, bad := range []string{`"速览"`, `["速览"]`, `null`} {
if w := write(bad); w.Page.Titles != nil || w.Panels[0].Titles != nil {
t.Errorf("titles %s: %v %v", bad, w.Page.Titles, w.Panels[0].Titles)
}
}
}