A plugin's page can now read one day's usage and share an image it
drew. This is for the day card (JungleyYeh on X asked for a "daily AI
statement"); the card itself is an optional community plugin,
@magpie-community/page-day-card.
- A page that asks for the "usage:day" read gets that day as
magpie.read("usage:day", {date, tz}): totals, each agent and its
models, the streak and the average active day (usageapi/day.go,
gui/pageday.go). The Plugins page names the read before it is granted.
- magpie.image.copy(png) and magpie.image.save(png, name) share a PNG
of at most 16 MB. In the window they go through
POST /api/plugin-pages/image (pageimage.go): copy puts the picture on
the clipboard (clipimage.go: osascript PNGf on macOS, PowerShell
PNG + Bitmap on Windows, wl-copy or xclip on Linux), and save writes
it to Downloads under a name made safe. In magpie web, the browser
saves or copies the picture itself.
- The window's side, pageshare.js, is imported the first time a page
shares a picture. The core grows by its one call only, paid for by
comments cut, so the budget is not raised.
Tests:
- New: TestDayOf, TestPluginPageReadsADay, TestPluginPageSharesAPicture,
TestPageImageName and TestClipImageCommand. plugin-page.test.cjs
covers the bridge, both engines and a refused SVG.
What was run (mote):
- gofmt, plus go vet on darwin, linux and windows: clean.
- Full go test ./... at the pre-rebase base: 369 packages ok, 3 FAILs.
- TestEveryRouteHasACommandOrIsNamed and TestCoreScriptBudget were
this change's own reds. The new route now has a parity entry, and
the share code moved into lazy pageshare.js with no raise.
- TestGatewayModeStatsNameTheGatewaySessions is #5690, the red in the
first hour of the day (it ran at 00:32).
- GUI suite (affected and plugin files, both engines): 1160 of 1179
passed. Every red is filed and none touches this change:
- i18n-keys "a plugin's words are its own": #5332, #5426, #5482.
- page-width-steady: #5013.
- plugin-card-update in WebKit: #3079.
- plugin-move-unserved "Try again": #4410.
- At this head, after the rebase on bc8b01d60:
- gofmt clean; go vet ./internal/... on darwin, linux (nogui) and
windows ok.
- internal/usageapi, internal/plugin and internal/cli ok.
- internal/gui: TestCoreScriptBudget, the TestPluginPage* tests and
the TestPageImage/TestClipImage tests pass.
- plugin-page and old-webkit (both engines): 12 of 12.
Real use:
- A sandbox HOME with a copy of real usage, through the real GUI
server, in Chromium and WebKit, light and dark, en/zh/zh-TW/ja/de, at
420 and 1100 px. The card showed 87.9M tokens, $9.85 and 4076 calls
with a 22-day streak.
- Picking an earlier day and WebKit's Copy ("Copied") worked, with no
page errors.
- Not tried at push: Chromium's Copy (its headless clipboard was
denied) and magpie web's Save as a captured download. A second real
run that grants both was still queued on mote's suite lock.
- Clipboard: checked live on macOS (osascript) and Windows
(PowerShell). Not tried live on Linux: WSLg had no display.
221 lines
8.6 KiB
Go
221 lines
8.6 KiB
Go
package gui
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"image"
|
|
"image/png"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// dayPlugin is pagePlugins' page asking for usage:day too.
|
|
func dayPlugin(t *testing.T) (on, off string) {
|
|
t.Helper()
|
|
on, off = pagePlugins(t)
|
|
pkg := `{"name":"magpie-usage-glance","version":"0.1.0","magpie":{"page":{"title":"Usage glance","file":"page/index.html","icon":"page/icon.svg"},"permissions":["usage:day"]}}`
|
|
if err := os.WriteFile(filepath.Join(on, "package.json"), []byte(pkg), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return on, off
|
|
}
|
|
|
|
func pagePost(h http.Handler, path string, body any) *httptest.ResponseRecorder {
|
|
b, _ := json.Marshal(body)
|
|
r := httptest.NewRequest("POST", path, bytes.NewReader(b))
|
|
r.Header.Set("Content-Type", "application/json")
|
|
return do(h, r)
|
|
}
|
|
|
|
// A page reads a day only when its package asks for usage:day, and the
|
|
// day it reads names its logos as data: pictures, a page's only kind.
|
|
func TestPluginPageReadsADay(t *testing.T) {
|
|
on, _ := dayPlugin(t)
|
|
h := pageMux()
|
|
w := pagePost(h, "/api/plugin-pages/read", map[string]any{"spec": on, "read": "usage:day", "params": map[string]any{}})
|
|
var day map[string]any
|
|
if w.Code != 200 || json.Unmarshal(w.Body.Bytes(), &day) != nil {
|
|
t.Fatalf("usage:day: %d %s", w.Code, w.Body)
|
|
}
|
|
for _, k := range []string{"date", "today", "agents", "days", "icons", "currency", "calls", "cost"} {
|
|
if _, ok := day[k]; !ok {
|
|
t.Errorf("usage:day has no %s: %s", k, w.Body)
|
|
}
|
|
}
|
|
if w := pagePost(h, "/api/plugin-pages/read", map[string]any{"spec": on, "read": "usage:day", "params": map[string]any{"date": "2999-01-01"}}); w.Code == 200 {
|
|
t.Errorf("a day after today: %d %s", w.Code, w.Body)
|
|
}
|
|
if w := pagePost(h, "/api/plugin-pages/read", map[string]any{"spec": on, "read": "usage:summary"}); w.Code != http.StatusForbidden {
|
|
t.Errorf("a read it no longer declares: %d", w.Code)
|
|
}
|
|
|
|
// a -color logo is drawn as it is, a plain one in the text's colour
|
|
if ic, ok := pageIconOf("claude-color"); !ok || ic.Mono || !strings.HasPrefix(ic.Src, "data:image/svg+xml;base64,") {
|
|
t.Errorf("claude's logo: %+v", ic)
|
|
}
|
|
if ic, ok := pageIconOf("openai"); !ok || !ic.Mono {
|
|
t.Errorf("openai's logo: %+v", ic)
|
|
}
|
|
if ic, ok := pageIconOf("alma"); !ok || ic.Mono || !strings.HasPrefix(ic.Src, "data:image/png;base64,") {
|
|
t.Errorf("alma's logo: %+v", ic)
|
|
}
|
|
for _, name := range []string{"", "generic", "../../app.js", "https://x.example/a.svg", `C:\a`, "no-such-logo"} {
|
|
if ic, ok := pageIconOf(name); ok {
|
|
t.Errorf("%q gave a logo %+v", name, ic)
|
|
}
|
|
}
|
|
}
|
|
|
|
func testPNG(t *testing.T, w, h int) []byte {
|
|
t.Helper()
|
|
var b bytes.Buffer
|
|
if err := png.Encode(&b, image.NewRGBA(image.Rect(0, 0, w, h))); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return b.Bytes()
|
|
}
|
|
|
|
// A page's picture is put on the clipboard or saved in Downloads, never
|
|
// over a file there; what isn't a PNG of a card's size, a page magpie
|
|
// doesn't show, an action of no name and a page that asks over and over
|
|
// are refused.
|
|
func TestPluginPageSharesAPicture(t *testing.T) {
|
|
on, off := dayPlugin(t)
|
|
dir := t.TempDir()
|
|
var copied [][]byte
|
|
heldCopy, heldDir := copyImage, pageImageDir
|
|
copyImage = func(_ context.Context, b []byte) error { copied = append(copied, b); return nil }
|
|
pageImageDir = func() string { return dir }
|
|
t.Cleanup(func() { copyImage, pageImageDir = heldCopy, heldDir })
|
|
pageImages.Lock()
|
|
clear(pageImages.at)
|
|
pageImages.Unlock()
|
|
h := pageMux()
|
|
pic := testPNG(t, 4, 3)
|
|
next := func() {
|
|
// past the page's half second
|
|
pageImages.Lock()
|
|
clear(pageImages.at)
|
|
pageImages.Unlock()
|
|
}
|
|
|
|
if w := pagePost(h, "/api/plugin-pages/image", map[string]any{"spec": on, "action": "copy", "png": pic}); w.Code != 200 || len(copied) != 1 || !bytes.Equal(copied[0], pic) {
|
|
t.Fatalf("copy: %d %s", w.Code, w.Body)
|
|
}
|
|
next()
|
|
var saved struct{ Path string }
|
|
w := pagePost(h, "/api/plugin-pages/image", map[string]any{"spec": on, "action": "save", "name": "magpie 2026-11-09", "png": pic})
|
|
if w.Code != 200 || json.Unmarshal(w.Body.Bytes(), &saved) != nil || filepath.Base(saved.Path) != "magpie 2026-11-09.png" {
|
|
t.Fatalf("save: %d %s", w.Code, w.Body)
|
|
}
|
|
if b, err := os.ReadFile(filepath.Join(dir, "magpie 2026-11-09.png")); err != nil || !bytes.Equal(b, pic) {
|
|
t.Errorf("saved: %v", err)
|
|
}
|
|
next()
|
|
w = pagePost(h, "/api/plugin-pages/image", map[string]any{"spec": on, "action": "save", "name": "magpie 2026-11-09.png", "png": pic})
|
|
if w.Code != 200 || !strings.HasSuffix(w.Body.String(), `magpie 2026-11-09-2.png"}`+"\n") {
|
|
t.Errorf("a second save: %d %s", w.Code, w.Body)
|
|
}
|
|
if w := pagePost(h, "/api/plugin-pages/image", map[string]any{"spec": on, "action": "copy", "png": pic}); w.Code != http.StatusTooManyRequests {
|
|
t.Errorf("within half a second: %d", w.Code)
|
|
}
|
|
|
|
for name, body := range map[string]map[string]any{
|
|
"another page": {"spec": off, "action": "copy", "png": pic},
|
|
"a made-up page": {"spec": "/nowhere", "action": "copy", "png": pic},
|
|
"no action": {"spec": on, "action": "open", "png": pic},
|
|
"not a PNG": {"spec": on, "action": "copy", "png": []byte("<svg/>")},
|
|
"nothing": {"spec": on, "action": "copy"},
|
|
"too wide a side": {"spec": on, "action": "copy", "png": testPNG(t, maxPageImageSide+1, 1)},
|
|
} {
|
|
next()
|
|
if w := pagePost(h, "/api/plugin-pages/image", body); w.Code == 200 {
|
|
t.Errorf("%s: %d", name, w.Code)
|
|
}
|
|
}
|
|
if len(copied) != 1 {
|
|
t.Errorf("copied %d times", len(copied))
|
|
}
|
|
next()
|
|
copyImage = func(context.Context, []byte) error { return errNoImageClipboard }
|
|
if w := pagePost(h, "/api/plugin-pages/image", map[string]any{"spec": on, "action": "copy", "png": pic}); w.Code == 200 || !strings.Contains(w.Body.String(), "xclip") {
|
|
t.Errorf("a copy that failed: %d %s", w.Code, w.Body)
|
|
}
|
|
// a body past the largest picture is cut off, not read whole
|
|
next()
|
|
big := `{"spec":` + strings.Repeat(" ", maxPageImage*4/3+128<<10) + `""}`
|
|
r := httptest.NewRequest("POST", "/api/plugin-pages/image", strings.NewReader(big))
|
|
if w := do(h, r); w.Code == 200 {
|
|
t.Errorf("an oversize body: %d", w.Code)
|
|
}
|
|
if !pageImageAllowed("y", time.Now()) || pageImageAllowed("y", time.Now()) || !pageImageAllowed("y", time.Now().Add(time.Second)) {
|
|
t.Error("pageImageAllowed")
|
|
}
|
|
}
|
|
|
|
func TestPageImageName(t *testing.T) {
|
|
for in, want := range map[string]string{
|
|
"": "magpie",
|
|
" ": "magpie",
|
|
"magpie 2026-11-09.png": "magpie 2026-11-09",
|
|
"../../etc/passwd": "etc-passwd",
|
|
`a:b*c?"d"<e>|f\g`: "a-b-c--d--e--f-g",
|
|
"x\ny\x00": "x-y",
|
|
"..": "magpie",
|
|
strings.Repeat("长", 100): strings.Repeat("长", 80),
|
|
} {
|
|
if got := pageImageName(in); got != want {
|
|
t.Errorf("pageImageName(%q) = %q, want %q", in, got, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
// Each system's own tool puts the picture on its clipboard; the file's
|
|
// path is an argument, never a part of a script, and a Linux with no tool
|
|
// says what to install.
|
|
func TestClipImageCommand(t *testing.T) {
|
|
const file = `/tmp/it's "x".png`
|
|
env := map[string]string{}
|
|
getenv := func(k string) string { return env[k] }
|
|
have := map[string]bool{}
|
|
look := func(n string) (string, error) {
|
|
if have[n] {
|
|
return "/usr/bin/" + n, nil
|
|
}
|
|
return "", errors.New("none")
|
|
}
|
|
name, args, _, err := clipImageCommand("darwin", getenv, look, file)
|
|
if err != nil || name != "osascript" || args[len(args)-1] != file || strings.Contains(strings.Join(args[:len(args)-1], " "), file) {
|
|
t.Errorf("darwin: %s %q %v", name, args, err)
|
|
}
|
|
name, args, extra, err := clipImageCommand("windows", getenv, look, file)
|
|
if err != nil || name != "powershell.exe" || !strings.Contains(strings.Join(args, " "), "-STA") || len(extra) != 1 || extra[0] != "MAGPIE_CLIP_PNG="+file || strings.Contains(strings.Join(args, " "), file) {
|
|
t.Errorf("windows: %s %q %q %v", name, args, extra, err)
|
|
}
|
|
if _, _, _, err := clipImageCommand("linux", getenv, look, file); !errors.Is(err, errNoImageClipboard) {
|
|
t.Errorf("linux with no tool: %v", err)
|
|
}
|
|
have["xclip"] = true
|
|
name, args, _, err = clipImageCommand("linux", getenv, look, file)
|
|
if err != nil || name != "/usr/bin/xclip" || args[len(args)-1] != file {
|
|
t.Errorf("linux xclip: %s %q %v", name, args, err)
|
|
}
|
|
// Wayland's own tool first, but xclip when it has none
|
|
env["WAYLAND_DISPLAY"] = "wayland-0"
|
|
if name, _, _, _ := clipImageCommand("linux", getenv, look, file); name != "/usr/bin/xclip" {
|
|
t.Errorf("wayland without wl-copy: %s", name)
|
|
}
|
|
have["wl-copy"] = true
|
|
name, args, _, err = clipImageCommand("linux", getenv, look, file)
|
|
if err != nil || name != "sh" || args[2] != "/usr/bin/wl-copy" || args[3] != file || strings.Contains(args[1], file) {
|
|
t.Errorf("wayland: %s %q %v", name, args, err)
|
|
}
|
|
}
|