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.
178 lines
6.8 KiB
Go
178 lines
6.8 KiB
Go
package gui
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/settings"
|
|
)
|
|
|
|
// copyTree copies a testdata folder to to.
|
|
func copyTree(t *testing.T, from, to string) {
|
|
t.Helper()
|
|
err := filepath.WalkDir(from, func(p string, d os.DirEntry, err error) error {
|
|
if err != nil {
|
|
return err
|
|
}
|
|
rel, _ := filepath.Rel(from, p)
|
|
if d.IsDir() {
|
|
return os.MkdirAll(filepath.Join(to, rel), 0o755)
|
|
}
|
|
b, err := os.ReadFile(p)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return os.WriteFile(filepath.Join(to, rel), b, 0o644)
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
// panelPlugins writes plugins.json with the example panel plugin
|
|
// (testdata/panel-plugin), on and a copy switched off, and two copies of
|
|
// the example page plugin that each ask to replace Usage; it answers
|
|
// their specs in that order.
|
|
func panelPlugins(t *testing.T) (on, off, first, second string) {
|
|
t.Helper()
|
|
h := t.TempDir()
|
|
t.Setenv("HOME", h)
|
|
t.Setenv("USERPROFILE", h)
|
|
t.Setenv("XDG_CONFIG_HOME", filepath.Join(h, ".config"))
|
|
t.Setenv("XDG_CACHE_HOME", filepath.Join(h, ".cache"))
|
|
t.Setenv("XDG_DATA_HOME", filepath.Join(h, ".local", "share"))
|
|
t.Setenv("MAGPIE_BUN", "")
|
|
t.Setenv("PATH", "")
|
|
on, off = filepath.Join(h, "src", "notes"), filepath.Join(h, "src", "notes-off")
|
|
first, second = filepath.Join(h, "src", "usage-a"), filepath.Join(h, "src", "usage-b")
|
|
copyTree(t, "testdata/panel-plugin", on)
|
|
copyTree(t, "testdata/panel-plugin", off)
|
|
for i, dir := range []string{first, second} {
|
|
copyTree(t, "testdata/page-plugin", dir)
|
|
pkg := `{"name":"usage-` + string(rune('a'+i)) + `","version":"0.1.0","magpie":{"page":{"title":"Usage ` + string(rune('A'+i)) + `","file":"page/index.html","replaces":"usage"},"permissions":["usage:summary"]}}`
|
|
if err := os.WriteFile(filepath.Join(dir, "package.json"), []byte(pkg), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
if err := os.WriteFile(filepath.Join(h, "secret.txt"), []byte("SECRET"), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.MkdirAll(settings.Dir(), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
b, _ := json.Marshal(map[string]any{"plugins": []map[string]any{{"spec": on}, {"spec": off, "off": true}, {"spec": first}, {"spec": second}}})
|
|
if err := os.WriteFile(filepath.Join(settings.Dir(), "plugins.json"), b, 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return on, off, first, second
|
|
}
|
|
|
|
// A plugin's panels are listed with the page each goes in, and each is
|
|
// served by a token of its own, shut in as a page is: to its own folder,
|
|
// off the network, and none of a plugin switched off.
|
|
func TestPluginPanelsServedShutIn(t *testing.T) {
|
|
on, off, _, _ := panelPlugins(t)
|
|
h := pageMux()
|
|
var panels []pluginPanel
|
|
w := do(h, httptest.NewRequest("GET", "/api/plugin-panels", nil))
|
|
if err := json.Unmarshal(w.Body.Bytes(), &panels); err != nil || len(panels) != 3 {
|
|
t.Fatalf("panels: %s %v", w.Body, err)
|
|
}
|
|
want := []struct{ slot, title, key string }{
|
|
{"provider", "Notes", "plugin-magpie-provider-notes-0"},
|
|
{"usage", "Top models today", "plugin-magpie-provider-notes-1"},
|
|
{"settings", "Provider notes", "plugin-magpie-provider-notes-2"},
|
|
}
|
|
toks := map[string]bool{}
|
|
for i, p := range panels {
|
|
if p.Spec != on || p.Slot != want[i].slot || p.Title != want[i].title || p.Key != want[i].key || len(p.Reads) != 1 || p.Reads[0] != "usage:summary" {
|
|
t.Errorf("panel %d: %+v", i, p)
|
|
}
|
|
tok := strings.Split(strings.TrimPrefix(p.URL, pluginPagePrefix), "/")[0]
|
|
if toks[tok] || tok == pageToken(on) {
|
|
t.Errorf("panel %d shares a token: %s", i, tok)
|
|
}
|
|
toks[tok] = true
|
|
r := httptest.NewRequest("GET", p.URL, nil)
|
|
r.Host = "127.0.0.1:3437"
|
|
w := do(h, r)
|
|
if w.Code != 200 || !strings.Contains(w.Body.String(), "<html") {
|
|
t.Fatalf("panel %d: %d %s", i, w.Code, w.Body)
|
|
}
|
|
csp := w.Header().Get("Content-Security-Policy")
|
|
for _, want := range []string{"sandbox allow-scripts", "connect-src 'none'", "script-src 127.0.0.1:3437" + pluginPagePrefix + tok + "/;"} {
|
|
if !strings.Contains(csp, want) {
|
|
t.Errorf("panel %d: policy %q has no %q", i, csp, want)
|
|
}
|
|
}
|
|
if strings.Contains(csp, "allow-same-origin") {
|
|
t.Errorf("panel %d: policy %q lets it out", i, csp)
|
|
}
|
|
for _, path := range []string{pluginPagePrefix + tok + "/../package.json", pluginPagePrefix + tok + "/../../secret.txt"} {
|
|
r := httptest.NewRequest("GET", "/", nil)
|
|
r.URL.Path = path
|
|
w := httptest.NewRecorder()
|
|
servePluginPage(w, r)
|
|
if w.Code == 200 || strings.Contains(w.Body.String(), "SECRET") || strings.Contains(w.Body.String(), `"name"`) {
|
|
t.Errorf("%s: %d %s", path, w.Code, w.Body)
|
|
}
|
|
}
|
|
}
|
|
// the switched-off copy's panels, by the token they would have
|
|
r := httptest.NewRequest("GET", "/", nil)
|
|
r.URL.Path = pluginPagePrefix + pageToken(off+"\x00panel\x000") + "/notes.html"
|
|
w = httptest.NewRecorder()
|
|
servePluginPage(w, r)
|
|
if w.Code == 200 {
|
|
t.Errorf("a plugin switched off's panel: %d", w.Code)
|
|
}
|
|
}
|
|
|
|
// A panels-only plugin reads what it declared and nothing else, through
|
|
// the same endpoint as a page, and sets its own options.
|
|
func TestPluginPanelsReadWhatTheyDeclared(t *testing.T) {
|
|
on, off, _, _ := panelPlugins(t)
|
|
h := pageMux()
|
|
post := func(path, body string) *httptest.ResponseRecorder {
|
|
r := httptest.NewRequest("POST", path, strings.NewReader(body))
|
|
r.Header.Set("Content-Type", "application/json")
|
|
return do(h, r)
|
|
}
|
|
spec, _ := json.Marshal(on)
|
|
if w := post("/api/plugin-pages/read", `{"spec":`+string(spec)+`,"read":"providers:list"}`); w.Code != http.StatusForbidden {
|
|
t.Errorf("an undeclared read: %d %s", w.Code, w.Body)
|
|
}
|
|
if w := post("/api/plugin-pages/read", `{"spec":`+string(spec)+`,"read":"usage:summary"}`); w.Code != 200 {
|
|
t.Errorf("a declared read: %d %s", w.Code, w.Body)
|
|
}
|
|
offSpec, _ := json.Marshal(off)
|
|
if w := post("/api/plugin-pages/read", `{"spec":`+string(offSpec)+`,"read":"usage:summary"}`); w.Code == 200 {
|
|
t.Errorf("a plugin switched off read: %d", w.Code)
|
|
}
|
|
if w := post("/api/plugin-pages/options", `{"spec":`+string(spec)+`,"options":{"notes":{"openai":"work"}}}`); w.Code != 200 || !strings.Contains(w.Body.String(), `"openai":"work"`) {
|
|
t.Errorf("options set: %d %s", w.Code, w.Body)
|
|
}
|
|
}
|
|
|
|
// Two plugins asking to replace one built-in page: the first in
|
|
// plugins.json's order takes its place, the other is a page of its own.
|
|
func TestPluginPageReplacesFirstWins(t *testing.T) {
|
|
_, _, first, second := panelPlugins(t)
|
|
var pages []pluginPage
|
|
w := do(pageMux(), httptest.NewRequest("GET", "/api/plugin-pages", nil))
|
|
if err := json.Unmarshal(w.Body.Bytes(), &pages); err != nil || len(pages) != 2 {
|
|
t.Fatalf("pages: %s %v", w.Body, err)
|
|
}
|
|
if pages[0].Spec != first || pages[0].Replaces != "usage" {
|
|
t.Errorf("the first: %+v", pages[0])
|
|
}
|
|
if pages[1].Spec != second || pages[1].Replaces != "" {
|
|
t.Errorf("the second: %+v", pages[1])
|
|
}
|
|
}
|